title: PHP Slim 3 - Accessing class object instances within a slim route
body: <p>So I’m learning how to write a Slim 3 PHP authentication app and I'm using an example code structure to get me started. The example code has a file called dependencies.php which has a series of functions that create object instances of other classes. These are then assigned to a $container variable with a name for each function. An example of these functions in the dependencies.php file can be seen here: </p>
  
  <pre><code>$container['view'] = function ($container) {
      $view = new \Slim\Views\Twig(
          $container['settings']['view']['template_path'],
          $container['settings']['view']['twig'],
          [
              'debug' =&gt; true // This line should enable debug mode
          ]
      );
  
      $basePath = rtrim(str_ireplace('index.php', '', $container['request']-&gt;getUri()-&gt;getBasePath()), '/');
      $view-&gt;addExtension(new Slim\Views\TwigExtension($container['router'], $basePath));
  
      $view-&gt;addExtension(new \Twig_Extension_Debug());
  
      return $view;
  };
  
  $container['validate_sanitize'] = function ($container)
  {
      $class_path = $container-&gt;get('settings')['class_path'];
      require $class_path . 'ValidateSanitize.php';
      $validator = new ValidateSanitize();
      return $validator;
  };
  
  $container['hash_password'] = function($container)
  {
      $class_path = $container-&gt;get('settings')['class_path'];
      require $class_path . 'HashPassword.php';
      $hash = new HashPassword();
      return $hash;
  };
  </code></pre>
  
  <p>These functions are then somehow called in my slim routes. For example in my <code>register.php</code> slim route the <code>validate_sanitize</code> class object is called with a simple </p>
  
  <pre><code>$this-&gt;get(‘validate_sanitize’); 
  </code></pre>
  
  <p>and assigned to a variable which I can then use to call methods from the <code>validate_sanitize</code> class. </p>
  
  <p>However what I don’t understand is how this <code>get</code> method works on calling a class object from the dependencies.php file. </p>
  
  <p>Here is the aforementioned register route that is a post request for incoming form data:</p>
  
  <pre><code>$app-&gt;post('/register', function(Request $request, Response $response)
  {
      $arr_tainted_params = $request-&gt;getParsedBody();
  
      $sanitizer_validator = $this-&gt;get('validate_sanitize'); //here for example
      $password_hasher = $this-&gt;get('hash_password');
  
      $tainted_email = $arr_tainted_params['email'];
      $tainted_username = $arr_tainted_params['username'];
      $tainted_password = $arr_tainted_params['password'];
  
      $model = $this-&gt;get('model');
      $sql_wrapper = $this-&gt;get('sql_wrapper');
      $sql_queries = $this-&gt;get('sql_queries');
      $db_handle = $this-&gt;get('dbase');
  
      $cleaned_email = $sanitizer_validator-&gt;sanitize_input($tainted_email, FILTER_SANITIZE_EMAIL);
      $cleaned_username = $sanitizer_validator-&gt;sanitize_input($tainted_username, FILTER_SANITIZE_STRING);
      $cleaned_password = $sanitizer_validator-&gt;sanitize_input($tainted_password, FILTER_SANITIZE_STRING);
   });
  </code></pre>
  
  <p>All my routes are contained within a routes.php file that looks like this:</p>
  
  <pre><code> &lt;?php
  
  require 'routes/change_password.php';
  require 'routes/forgot_password.php';
  require 'routes/homepage.php';
  require 'routes/login.php';
  require 'routes/logout.php';
  require 'routes/register.php';
  </code></pre>
  
  <p>There is also a bootstrap file that creates a new Slim container, Slim App instance and also includes necessary files. I’m also not entirely sure what a Slim\Container is or what it does. This bootstrap file looks like this:</p>
  
  <pre><code>&lt;?php
  
  session_start();
  
  require __DIR__ . '/../vendor/autoload.php';
  
  $settings = require __DIR__ . '/app/settings.php'; //an array of options containing database configurations and the path to twig templates
  
  $container = new \Slim\Container($settings); //not sure what this does
  
  require __DIR__ . '/app/dependencies.php';
  
  $app = new \Slim\App($container);
  
  require __DIR__ . '/app/routes.php';
  
  $app→run();
  </code></pre>
  
  <p>I’ve tried reading a number of articles as well as watching various YouTube videos but they do things differently with controllers which just adds more complexity and confusion for me. I’d much rather an explanation on this specific example as I find the code structure fairly simple. </p>
  
  <p>Thanks. </p>