Skip to content

[5.0] Edited: Illuminate\Routing\Stack always uses $this->request instead of the passed $request at the end of the initial Middleware stack. #6287

@Aaron3

Description

@Aaron3

Is this intentional? It seems that Middleware should be able to change the request going in and out of it. It can pass along changes to other Middleware but after all the Middleware is run, Laravel ignores the changes on the initial request.

This Middleware runs on every request:

class GetLocale implements Middleware {
    public function handle($request, Closure $next)
    {
        //url: site.com/en/someurl/
        $modifiedRequest = $request->duplicate();
        $language = $request->segment(1);
        if(is_valid_language($language))
        {
            //do stuff with $language
            $modifiedRequest->server->set('REQUEST_URI', strip_language($request->path());
            //url: site.com/someurl/
            return $next($modifiedRequest);
        }
    }
}

After GetLocale runs, the other Middleware functions as I would expect but \Illuminate\Routing\Stack::then() returns the original request because of getInitialSlice and a route not found exception is thrown because site.com/en/someurl/ doesn't actually exist.

Editing Illuminate\Routing\Stack to use a passed $request rather than $this->request everything function as expected and site.com/en/someurl/ resolves transparently to site.com/someurl/ .

The following works exactly as I'd expect.

    protected function getInitialSlice(Closure $app)
    {
        return function($request) use ($app)
        {
            return call_user_func($app, $request);
        };
    }

On Middleware run later and not on every load, it always uses the passed $request at the end:

return (new Stack($this->container))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions