Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mr_hitss's avatar

Laravel - Override the Router / Route

Does anyone know how to override the Router in Laravel 10. I tried by creating a class and extends the Routers and added some custom functions. And in the RouteServiceProvider boot that new Route Service using singleton. But it didn't worked.

0 likes
4 replies
tykus's avatar

The Router is macroable; whatever custom functions you need should be achievable using macros

puklipo's avatar

Write Why do you want to do this?

No need to override for most use cases.

If you just want to add a custom function, you can do it with macro.

// AppServiceProvider

use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Route;

    public function boot(): void
    {
        Route::macro('custom', function () {
            //
        });
    }

edit

tykus's avatar

@mr_hitss does editable and removable mean the Route action has side-effects? If so, then they would not be suitable for a GET Requests!

Aside, what does type achieve that could not be done with appropriate application of a Middleware?

Please or to participate in this conversation.