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

_zac_'s avatar
Level 12

URL Parameter in Route name

I cannot find anywhere that it is possible to use a URL Parameter in a Route name. Is anyone aware of a way to do this?

Route::get('/{action}',Controller::class)->name($action};

This would be useful if building a single controller file for Livewire.

0 likes
4 replies
LaryAI's avatar
Level 58

In Laravel, route names are typically static and are used to generate URLs or refer to routes in a consistent manner. You cannot directly use a URL parameter as a route name in the way you've described. However, you can dynamically generate route names in your application logic if needed.

If you want to dynamically generate URLs based on parameters, you can use route parameters and then build URLs using those parameters. Here's an example of how you might handle this:

  1. Define your route with a parameter:
Route::get('/{action}', [Controller::class, 'handleAction'])->name('action.route');
  1. In your controller, you can handle the action:
class Controller extends BaseController
{
    public function handleAction($action)
    {
        // Handle the action here
        return view('your-view', ['action' => $action]);
    }
}
  1. If you need to generate a URL with a specific action, you can use the route() helper and pass the parameter:
$url = route('action.route', ['action' => 'your-action']);

This approach allows you to keep your route names static while still using dynamic parameters in your URLs. If you need to refer to different actions, you can pass those as parameters when generating URLs or handling requests.

MouteeSabouni's avatar

Route names are typically defined as static strings, which means you cannot directly use a URL parameter as part of the route name, like in your example. However, if you want to use them for whatever reason you have, you may use $request->route.

use Illuminate\Support\Facades\Route;

Route::get('/{action}', [Controller::class, 'method'])
    ->middleware(function ($request, $next) {
        $action = $request->route('action');

        Route::current()->setName("action.{$action}");

        return $next($request);
    });

In the example above, we used the middleware() as a way to intercept the request and dynamically set the route name based on the URL parameter before the request is handled by the controller.

Anyway, if you want, you may post your Livewire code, and we can figure something out because this approach is, of course, not recommended.

Have a nice day!

1 like
Snapey's avatar

I don't see the point. Don't use names in this case.

If you want to go to /home and have home passed to a single Livewire router (for some unfathonable reason) then just specify the route and capture it as a parameter.

This does not work for the base route though.

This would work;

Route::get('/page/{action}, Controller::class);

and links like;

<a href="/page/home">HOME</a>
<a href="/page/contact">CONTACT</a>

These would pass home or contact into Controller as $action

_zac_'s avatar
Level 12

Thanks all for your replies. The solution I needed was:

$this->redirectRoute(name: 'admin.classes', parameters: ['action' => 'index'], navigate: true);

Please or to participate in this conversation.