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

timdr's avatar
Level 1

Laravel Route manipulation

Hello, i have a project where i define routes just as normal in the routes.php.

However, the admin should have the possibility to change route names (e.g. /forum/discussions to /talk) To test if this could work, i wrote following code snippet inside of a controller:

$customerRoute = Route::getRoutes()->getByName('admin.customer.index'); $customerRoute->setUri("admin/kunden");

This works in form of changing the route URI in the navbar, but when it comes to resolving the url after the admin clicked on it, the route is (of course) not found by the laravel router resulting in http 404. However, in the routeServiceProvider boot method the routes of my routes.php do not exist yet, so i cant manipulate them there...

Does anyone has an idea on how to solve this issue? Thanks a lot!

0 likes
4 replies
Niush's avatar

Theoretically, you could store custom routes in database. With fields like: id, route_name, url.

Then with Laravel fallback routes, you can find url in database with current request url and redirect to that route. If it does not exist return 404.

https://laravel.com/docs/9.x/routing#fallback-routes

But, I would not suggest this, because:

The whole approach is not great. Redirection and changes in route in not good for the SEO. Also, this won't work as expected in POST methods. And, route caching in Laravel will not happen.

timdr's avatar
Level 1

@Niush Thanks for your answer. However, this is not what i am exactly looking for. The default routes MUST be defined just as normal in the routes.php (because the app has a module system for external developers)

However, it would be nice if i could have kindof a middleware right after all the routes have been registered to modify them if needed. Those URL modifications would be static to customize the behaviour of the web app, therefore SEO should not be a problem.

Niush's avatar
Niush
Best Answer
Level 50

EDIT: I updated with probably what you want at the very end.

@timdr Ok, so normal assumption would be that routes.php when finds a match stops and sends the response.

But, instead it goes through all the registered route files, so, you don't need the middleware like approach.

So, first you might want to register new route to the boot method of RouteServiceProvider:

$this->routes(function () {
        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/routes.php'));

        //......
			
		Route::middleware('web')
		    ->namespace($this->namespace)
			->group(base_path('routes/url_modifier.php')); // This new route handler
});

Then, in the url_modifier.php file, you can register custom routes overriding the route names from routes.php. There, you can either get custom routes from database, or manually. Here is what I tested:

// /routes/url_modifier.php
<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

$runme = function () {
   Route::get('/test', function () {
       return "Overridden Content";
   })->name("test");	
};

$runme();

The route name test from routes.php is overridden properly, with this new route.

I would also suggest, to run artisan route:cache every time when admin makes changes to the custom routes. (using the Artisan Facade programmatically)

Note: The setUri function that you tried probably won't register permanently. While, with this way you have to re-assign the controller actions. I could not find any better solution yet.

Update:

This is probably what you exactly want. In the url_modifier.php use this:

$all_routes = collect(Route::getRoutes()->get());

$test = $all_routes->first(function ($value, $key) {
    return isset($value->action['as']) && $value->action['as'] === "test"; // The route name
});

$test->setUri("something-else"); // Re-set the URL to custom

Again, make sure to run route:cache so that this overhead does not slow down the application.

1 like
timdr's avatar
Level 1

@Niush Hi Niush, that did the trick! Thanks a lot for your help!

Please or to participate in this conversation.