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

SyedUmair's avatar

How can i pass more one optional parameter in the route

i am trying to pass more then one optional Parameter in my route

    'middleware' => 'auth:api',
    'prefix' => 'api/v1'
], function () use ($router) {
    $router->get('import[/{name}/{info}]','importController@index');
});

if info is empty i get a 404 i am using lumen 8

0 likes
13 replies
rodrigo.pedra's avatar

Per documentation here:

https://laravel.com/docs/8.x/routing#parameters-optional-parameters

You can use the ? to note a parameter is optional.

So in your example it would be something like this:

$router->get('import[/{name}/{info?}]','importController@index');

Also your controller action should have a default value when this value is missing:

// ImportController.php

public function index($name, $info = null)
{
    // ...
}
treii28's avatar

@Sinnbeck

I figured that out right quick when I came upon this example, however, I came up with an alternative that does work in some cases in vanilla laravel.

In my case, I have default values for a store api and a particular listing i'm looking for in my default store. So I want a 'find listings' route that defaults the items omitted. I should note, that optional items using this method requires them to be 'in order'. i.e. you can't leave out the first item if you include the second. It's kinda similar how stuff like C++ allows multiple definitions for a function:

My routes for getting shop details only needs the shopid to be optional. Getting shop listings for a specific listingid, I need both listingid and shopid but assume if I get just a listingid, it's for my default shop. If I get neither, it's for the default listing in my default shop:

Route::get('/getshop/{shopid?}', [EtsyapiController::class, 'getshop'])->name('etsyadmin.getshop');

// need to define two separate routes:
//     one for nothing with optional listingid
//     a second for with listingid with optional shopid
Route::get('/getlistings/{listingid?}', [EtsyapiController::class, 'getlistings']);
Route::get('/getlistings/{listingid}/{shopid?}', [EtsyapiController::class, 'getlistings']);

It should be noted that I'm defaulting my variables in the controller action to ='' then checking if they are empty to substitute a value from .env

It might get interesting if you try to name routes for redirects somewhere though. Maybe name one 'withlistingid' or something.

treii28's avatar

I should also note, that ordering might not be ideal for other applications. The underlying api method puts shopid first, listingid second. In my case, I may want to look up another item in my same default shop.

i.e. this method won't allow for 'only' shopid with no listingid

rodrigo.pedra's avatar

@sinnbeck you are correct, I didn't see the Lumen tag. Thanks for the heads up

@umair33 , following @sinnbeck advice and lumen documentation, you can try this if only the second parameter is optional:

$router->get('import/{name}[/{info}]','importController@index');

Or if both are optional, you can define more than one route to a controller action:

$router->get('import[/{name}]','importController@index');
$router->get('import/{name}[/{info}]','importController@index');

Don't forget to set default values for optional parameters

Sinnbeck's avatar

I was thinking like this.. Edit : beat me to it :p

    'middleware' => 'auth:api',
    'prefix' => 'api/v1'
], function () use ($router) {
    $router->get('import[/{name}]','importController@index');
    $router->get('import/{name}[/{info}]','importController@index');
});
1 like
rodrigo.pedra's avatar
Level 56

Are both parameters optional?

If only info is optional use just this one:

$router->get('import/{name}[/{info}]','importController@index');

If both are optional you can try this:

$router->get('import[/{name}[/{info}]]','importController@index');

Reference:

https://github.com/nikic/FastRoute

// Multiple nested optional parts are possible as well

$r->addRoute('GET', '/user[/{id:\d+}[/{name}]]', 'handler');

1 like
rodrigo.pedra's avatar

Note, Lumen uses nikic/FastRoute as its routing engine, but you should register your routes using Lumen API for that.

The quote in my last comment was just to highlight how to define a route with multiple optional arguments.

SyedUmair's avatar

thank you this works for me

$router->get('import[/{name}[/{info}]]','importController@index');

1 like

Please or to participate in this conversation.