Lumen-framework: Helper route() don't recognize optional params

Created on 22 Dec 2016  路  24Comments  路  Source: laravel/lumen-framework

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

bug

Most helpful comment

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.

$app->post('/test[/{name}]', function($name = null, Request $request) {
});

All 24 comments

Can confirm the issue

I don't see an optional param in your route?

@bobbybouwmann what about [/{format}] part? :)

@mstaack It's not optional, right now it's a required param. You need to add a question mark to make it optional

$app->get('{status}/{key}[/{format?}]', 'NfeController@show');

hmm okay.. had a similiar issue.

fast route docs state:

    // The /{title} suffix is optional
    $r->addRoute('GET', '/articles/{id:\d+}[/{title}]', 'get_article_handler');

so lumen handles this differently?

..so only braces....but lumen needs question mark?

There might be missing something in the docs...

Anyway the routes in Lumen work the same as the routes in Laravel, so you should be fine with the documentation from Laravel: https://laravel.com/docs/5.3/routing#parameters-optional-parameters

Anyway the routes in Lumen work the same as the routes in Laravel

Not quite true. The routing system in Lumen is actually entirely different from Laravel's internally. It's designed so feels the same to use though. Not everything the Laravel docs perfectly carries over.

The Lumen docs are available at: https://lumen.laravel.com/docs/5.3/routing.

@GrahamCampbell So there is no optional option for routes in Lumen? At least I can't find anything in the documentation about it

I don't know off the top of my head. You'd have to check the source code.

must be something around here:
https://github.com/laravel/lumen-framework/blob/5.3/src/Routing/UrlGenerator.php#L213

        $uri = preg_replace_callback('/\{(.*?)(:.*?)?(\{[0-9,]+\})?\}/', function ($m) use (&$parameters) {
            return isset($parameters[$m[1]]) ? array_pull($parameters, $m[1]) : $m[0];
        }, $uri);

does this work with [{optional-param}?] ?

whats the status here?

@themsaid can you have a look here?

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.

$app->post('/test[/{name}]', function($name = null, Request $request) {
});

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) {}

I have a similar problem. But the above decision seems to me doubtful.
Are there any progress in this problem?

I can confirm that this is a bug and can reproduce it. Open for PR's to fix this.

@driesvints Do you think this could be solved in the regex in the route method in UrlGenerator.php?

@JacksonIV saw your PR and yeah I believe so. Just need to figure out the regex I suppose 馃槄

@driesvints Yeah, i closed the PR myself because the tests failed.

@driesvints I tried a few things, but i don't know how to solve this neatly. Have you got any thoughts on this?

@JacksonIV I have this working in a branch but the pull request was closed (#828). Not sure what alternative solution would be preferred.

@yuloh I've opened a new Pr.

This should be fixed in the next release.

馃殌

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GGNB picture GGNB  路  5Comments

concept47 picture concept47  路  3Comments

soderluk picture soderluk  路  3Comments

matthewsuan picture matthewsuan  路  3Comments

Andrei-Vakulski picture Andrei-Vakulski  路  3Comments