[solved] Application middleware not dispatching routes


#1

Hy all I’m trying to implements an application middleware
I got the example from documentatio

class simpleAuth
{
    /**
     * Example middleware invokable class
     *
     * @param  \Psr\Http\Message\ServerRequestInterface $request  PSR7 request
     * @param  \Psr\Http\Message\ResponseInterface      $response PSR7 response
     * @param  callable                                 $next     Next middleware
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function __invoke($request,$response,$next)
    {
	//doing some stuffs... without modifing response
	return $response;
    }
}

middleware is added with

$app->add( new \simpleAuth());
$app->run();

the middleware is invoked but the route is not dispatched
if I comment the ->add route is dipatched normally

what I have missed ?


#2

You’re returning the response before you pass it down the rest of the middleware chain towards the app. Your class should look more like this:

class simpleAuth
{
    /**
     * Example middleware invokable class
     *
     * @param  \Psr\Http\Message\ServerRequestInterface $request  PSR7 request
     * @param  \Psr\Http\Message\ResponseInterface      $response PSR7 response
     * @param  callable                                 $next     Next middleware
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function __invoke($request,$response,$next)
    {
        // do some stuff
        $response = $next($request, $response);

        return $response;
    }
}