title: Ho to Implement Slim like middleware mechanism
body: <p>I decided to implement my own small framework to implement such stuff like dependency injection etc.</p>
  
  <p>Now I'm stucking at my middleware implementation. I can add middleware to a route but I im wondering how slim loops through the attached middleware.</p>
  
  <p>I'd like to do it the slim way, so in every middleware I can return a request or a response or the next middleware. But how do I have too iterate over my attached middleware.</p>
  
  <p>Here is my stack I want to proceed</p>
  
  <pre><code>class MiddlewareStack
  {
  
      private $stack;
  
      public function addMiddleware(Middleware $middleware)
      {
          $this-&gt;stack[] = $middleware;
      }
  
      public function processMiddleware(Request $request, Response $response)
      {
      }
  }
  </code></pre>
  
  <p>and thats the middleware interface </p>
  
  <pre><code>public function __invoke(Request $request, Response $response, $next);
  </code></pre>
  
  <p>I want to  </p>
  
  <pre><code>return $next($request,$response); 
  </code></pre>
  
  <p>in my middleware classes or just a response or a request.</p>
  
  <p>Here's how to create middlware callable in slim.</p>
  
  <p><a href="http://www.slimframework.com/docs/concepts/middleware.html#invokable-class-middleware-example" rel="nofollow">http://www.slimframework.com/docs/concepts/middleware.html#invokable-class-middleware-example</a></p>