V4n1ll4
V4n1ll4

Reputation: 6099

Dynamic routing in Laravel 5.1

How can I do the following in Laravel 5.1:

Route::get('/users/{user}', ['as' => $user, 'uses' => 'UserController@index']);

The code above gives an error:

Undefined variable: user

Upvotes: 0

Views: 460

Answers (1)

CDF
CDF

Reputation: 194

Routing fails because you defined a $user variable as the route name, so Laravel returns an error.

Route names are useful for reverse routing, as an example when you define redirect or action attribute in your form.

Check the documentation to know how to pass variable by routes.

EDIT: Here the link to the doc for Laravel 5.1 ( which is similar to previous version btw ). A good practice is passibg the variable or an array of variables using a closure.

Route::get( '/users/{user}', function( $user ) {
return $user;});

And this one using correct route naming:

Route::get('/users/{user}', ['as' => 'userroute', function( $user ) {
return $user;}]);

http://laravel.com/docs/5.1/routing#route-parameters

Upvotes: 1

Related Questions