Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: why every route is executed twice?

Tags:

laravel

In my laravel app, I noticed that every route is executed twice, and can't figure out why

for example:

Route::get('called_twice', function () {
   dump('---');
});

return string '---' twice

enter image description here

Edit:

trying to backtrace the source of the issue, I put a dump in file

src/Illuminate/Foundation/Http/Kernel.php

protected function sendRequestThroughRouter($request)
{
    $this->app->instance('request', $request);

    Facade::clearResolvedInstance('request');

    $this->bootstrap();
dump('kernel');
    return (new Pipeline($this->app))
                ->send($request)
                ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
                ->then($this->dispatchToRouter());
}

and another dump in the constructor of the file

src/Illuminate/Pipeline/Pipeline.php

    public function __construct(Container $container = null)
{
    dump('pipeline');
    $this->container = $container;
}

and I get this:

enter image description here

the Pipeline class is called many time

Laravel 6.8.0


1 Answers

I think $next($request) is probably called twice in a middleware. The reason is that the response is passed throught each middleware (pipes) before it is returned back. So if $next($request) is called twice in one middleware it is normal that all pipes will be called again.

like image 117
Naco Avatar answered Oct 20 '25 17:10

Naco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!