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

pavsid's avatar

Default route parameter values

Hi, i'm trying to find out how to set the default value of a route parameter, so that if it doesn't exist when building the route, the default value is used in its place.

There's a section here http://laravel.com/docs/5.0/routing#route-parameters under "Optional Route Parameters With Default Value" which suggests you can set default values, but can't see how to set them in named routes.

0 likes
9 replies
pmall's avatar
class FooController extends Controller
{
    public function index($optional_parameter = 'default value')
    {
        // ...
    }
}
pavsid's avatar

@pmall thanks, but that doesn't help when building the route.

Basically, I have the following routes..

Route::get('inbox', [
            'as' => '',
            'uses' => 'MessagesController@inbox'
        ]);
        Route::get('sent', [
            'as' => '.sent',
            'uses' => 'MessagesController@sent'
        ]);
        Route::get('trash', [
            'as' => '.trash',
            'uses' => 'MessagesController@trash'
        ]);

and to read a message, the following route..

Route::get('{box}/read/{id}', [
            'as' => '.read',
            'uses' => 'MessagesController@read'
        ])->where('box', 'inbox|sent|trash')

But when building the read route, from within one of the other routes, the box parameter isn't defined. So i'd like to set the default value in the route definition.

Maybe i'm asking for too much, and should just set the value in the controller action, but I wanted a more flexible way of doing it.

1 like
pmall's avatar

If you are in the inbox view, you know it is inbox

pavsid's avatar

OK, i think i'm over-complicating it.

I'll start again...

When building a route URL using route(), do you have to explicitly define any route parameters every time, or can you set a default value in the definition?

Something like..

Route::get('/messages/{something}', [
            'as' => '',
            'uses' => 'MessagesController@inbox'
        ])
->defaults('something', 'inbox');

.. but that doesn't seem to work...

pmall's avatar

You dont even need a default value because it depends of the page you are on.

In inbox view :

route('messages.read', ['inbox', $id]);

In sent view :

route('messages.read', ['sent', $id]);

Etc

pavsid's avatar

Yeh, if you are using separate views, but how about if you are re-using views, or partials, etc. It would be good to be able to get the parameter out of the route, no?

pmall's avatar

Just structure your app so it is easy to manage routes ^^

seydu's avatar

@pmall. His questions are legitimate. But you are answering like he is doing things wrong. Laravel Route is using Symfony Route under the hood and it allows to set default parameter values, and other options that the default router will not use. Devs use these possibilities in various ways ( like setting an option that indicates that a given route needs to be exported in Javascript).

The last time I had a look at how the named routes are handled, the routing system just extracts the action and forgets about the remaining attributes.

1 like
pmall's avatar

His questions are legitimate. But you are answering like he is doing things wrong.

I think I misunderstood his question at the time

Please or to participate in this conversation.