title: Slim pass attribute with request
body: <p>I am creating a simple api with <a href="http://www.slimframework.com/" rel="nofollow noreferrer">slim framework</a> . </p>
  
  <p>I wanted to create a middleware to check if the token passed to the application is valid and not expired. </p>
  
  <p>This is the middleware:</p>
  
  <pre><code>public function __invoke(Request $request, Response $response, $next)
  {
      $token = $request-&gt;getParam('token', null);
      if (is_null($token)) {
          throw new NoTokenException();
      }
  
      /** @var Auth $authModule */
      $authModule = $this-&gt;getContainer()-&gt;get('AuthModule');
      $tokenModel = $authModule-&gt;validateToken($token);
  
      $request = $request-&gt;withAttribute('user_token_data', $tokenModel-&gt;getAttributes());
  
      return $next($request, $response);
  }
  </code></pre>
  
  <p>It's pretty simple. I validate the token and add new attribute to the $request parameter. </p>
  
  <p>In my routes I've done the following:</p>
  
  <pre><code>$app-&gt;post('/test', function($request, $response, $args){
  
      /** @var \Controller\TestController $testController */
      $testController = $this-&gt;get('TestController');
  
      $testController-&gt;testFunction();
  
  
  })-&gt;add(new \Middleware\Test\SomeOtheroMiddleware($app-&gt;getContainer()))
    -&gt;add(new \Middleware\AuthMiddleware($app-&gt;getContainer()));
  </code></pre>
  
  <p>In the route callback if i do <code>$request-&gt;getAttribute('user_token_data')</code> everything is in place.</p>
  
  <p>But since my controller is loaded with the slim container I want to pass over the <code>$request</code> variable in the <code>__constructor()</code></p>
  
  <p>Here is my container:</p>
  
  <pre><code>$container['TestController'] = function ($container) {
      $testModule = $container-&gt;get('TestModule');
      $request = $container-&gt;get('request');
      return new \Controller\TestController($testModule, $container, $request);
  };
  </code></pre>
  
  <p>Everything is loading, no errors, but in my <code>TestController-&gt;testFunction()</code> when I try to access the <code>$request</code> variable which I passed over with the container, I don't have the <code>user_token_data</code> key.</p>
  
  <p>I can create a setter for the controller to change the <code>$request</code> in the routes, but this doesn't sound like a good idea.</p>
  
  <p>I would like to know why the <code>$request</code> from the container, is not the same instance. And how can I do this without a setter.</p>
  
  <p>Any help would be appreciated.  </p>