lumen-framework: Helper route() don't recognize optional params
Hi, friends.
I’m trying to use the route() helper as the example:
NfeController@create
$urlParams = ['emitterId' => $emitter->id, 'status' => 'entradas', 'key' => $nfe->chave, 'format' => 'xml'];
$url = route('nfe', $urlParams);
routes.php
$app->group(['prefix' => 'emitter'], function($app){
$app->group(['prefix' => '{emitterId}/nfe'], function($app){
$app->get('{status}/{key}[/{format}]', ['as' => 'nfe', 'uses' => 'NfeController@show']);
}
}
but the function returns: http://localhost:8000/emitter/36/nfe/entradas/29161207683985000146550010000000151000000151[/xml]
The helper route supports optional params?
P.S.: lumen 5.3; nikic/fast-route: 1.0.1
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 3
- Comments: 24 (12 by maintainers)
You can use an optional route param but only at the end of the route. Lumen uses https://github.com/nikic/FastRoute so check that package out to see more about optional params.
This would work for only one optional param.
I have a similar problem. But the above decision seems to me doubtful. Are there any progress in this problem?
the question stays on… you have the optional parameter:
$app->get('/test[/{name}]', ['as' => 'test', 'uses' => 'MyController@test']);the issue’s title says: “Helper route()…” the thing is, if you try to use the helper route(), like:
route('test')route('test', ['name' => 'myname'])it supposed to return (like laravel’s optional param): “http://yourdomain.com/test” “http://yourdomain.com/test/myname”
but it’s returning: “http://yourdomain.com/test[/{name}]” “http://yourdomain.com/test[/myname]”
EDIT: @vinisouza if it would help, my solution for that problem, i’ve created two routes, with different names using the same controller action:
$app->get('{status}/{key}', ['as' => 'nfe', 'uses' => 'NfeController@show']);$app->get('{status}/{key}/{format}', ['as' => 'nfe.format', 'uses' => 'NfeController@show']);PS: dont forget to add a default value for $format in your controller:
public function show(Request $request, $format = null) {}@GrahamCampbell So there is no optional option for routes in Lumen? At least I can’t find anything in the documentation about it
@mstaack It’s not optional, right now it’s a required param. You need to add a question mark to make it optional