Skip to content

New route caching breaks usage of the custom Route class #32415

Closed
@klimov-paul

Description

@klimov-paul
  • Laravel Version: 7.2.1
  • PHP Version: 7.3.13
  • Database Driver & Version: *

Description:

New route caching system, which uses CompiledRouteCollection breaks usage of custom Route class. It breaks packages and solutions, which customize routing, like illuminatech/url-trailing-slash.

Steps To Reproduce:

  • declare custom MyRouter class extending standard Illuminate\Routing\Router

  • declare custom MyRoute class extending standard Illuminate\Routing\Route with some custom logic.

  • override newRoute() method for the router in the way it instantiates custom MyRoute instead of standard one

  • define some routes

  • use php artisan route:cache to cache the routes

As the result all additional logic placed in MyRoute is lost.

Code snippet

class MyRoute extends Illuminate\Routing\Route
{
    public function run()
    {
        // custom run logic
        // echo something for the test
        echo __METHOD__; // works only if routes are not cached
        return parent::run();
    }
}

class MyRoute extends Illuminate\Routing\Router
{
    protected function newRoute($methods, $uri, $action)
    {
        return (new MyRoute($methods, $uri, $action))
            ->setRouter($this)
            ->setContainer($this->container);
    }
}

Relates to illuminatech/url-trailing-slash#4

Suggestion

CompiledRouteCollection::newRoute() should invoke Router::newRoute() inside of it, so any custom route creation logic is not lost.
This however requires to change visibility of the method from protected to public.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions