title: Error Using Middleware in SLIM Framework
body: <p>I have been at this for hours now and can not seem to figure out why it's not working. This my first time using SLIM and my first exposure to middleware. I am trying to follow the <a href="https://www.slimframework.com/docs/concepts/middleware.html" rel="nofollow noreferrer">tutorial</a> listed on the slim website but just can't get to work. </p>
  
  <p>My bootstrap code: </p>
  
  <pre><code>&lt;?php
  
    require '../vendor/autoload.php';
  
    $app = new Slim\Slim();
  
  
    $app-&gt;get('/test', function() {
  
      echo 'Hello, World'; 
   }); 
  
  $mw = function ($request, $response, $next) {
      $response-&gt;getBody()-&gt;write('BEFORE');
      $response = $next($request, $response);
      $response-&gt;getBody()-&gt;write('AFTER');
  
      return $response;
  };
  
  
  $app-&gt;add($mw); 
  $app-&gt;run(); 
  </code></pre>
  
  <p>When I run just my slim url without the middleware it runs fine. I get <code>Hello, World</code> as the output when I runt <a href="http://mysite/test" rel="nofollow noreferrer">http://mysite/test</a>. But when I add the middleware code as listed on the slim site I get the following error:</p>
  
  <pre><code>Catchable fatal error: Argument 1 passed to Slim\Slim::add() must be an instance of Slim\Middleware, instance of Closure given, called in /Applications/XAMPP/xamppfiles/htdocs/project/api/public/index.php on line 22 and defined in /Applications/XAMPP/xamppfiles/htdocs/academy/api/vendor/slim/slim/Slim/Slim.php on line 1267
  </code></pre>
  
  <p>Am I missing something? Does the middleware require some other setup? The slim documentation is not very helpful when it comes to this. Any help appreciated.</p>