wriggler's avatar

Lumen routing parameters not (always) passed

Hi,

I'm having some trouble with Lumen's routing. I'm trying to capture the uri using regex in a route.

When I try to pass the captured data to a controller method, the variable is empty, yet when I do the same using a closure, the variable shows as expected. Here's the code:

This works:

$app->get('/{categoryUrlPath:[a-zA-Z0-9\-\/]+}', function($categoryUrlPath) {
  echo $categoryUrlPath;
});

This doesn't work:

$app->get('{categoryUrlPath:[a-zA-Z0-9\-\/]+}', ['uses' => 'App\Http\Controllers\FrontController@showSearch']);

and

public function showSearch($categoryUrlPath = null) {
    return $categoryUrlPath;    
}

Debugging the controller with

print_r(app('request')->route());

shows that the variable is there:

Array ( [0] => 1 [1] => Array ( [uses] => App\Http\Controllers\FrontController@showSearch ) [2] => Array ( [categoryUrlPath] => this/is/a/captured/uri ) ) 

But still it isn't passed as $categoryUrlPath

Any help appreciated - I'm puzzled. Also, I realise the are other (easier) ways to capture just the URI but I have a specific reason for doing it this way and, besides, I'd like to understand what I'm doing wrong with the router here.

Thanks!

0 likes
5 replies
bobbybouwmann's avatar

I noticed that in your closure you have a / before your route, but you don't have that when you use a controller! I don't know if that's the issue though...

wriggler's avatar

Hi blackbird,

The presence or not of the / makes no difference, unfortunately.

arabsight's avatar
Level 10

I just tried your code its the '= null' that's causing the problem (no idea why)

public function showSearch($path) [} // this works
public function showSearch($path = null) [} // this doesn't
1 like
wriggler's avatar

@arabsight - you're correct. Strangely, I tried the route without the '= null' many times before, but had the error mentioned. Now it works, as you say, when I take the '= null' away.

dertechniker's avatar

Just came here through a google search, and for me a ' = NULL' DOES work...

Please or to participate in this conversation.