php - How to use optional values in Laravel routes? -
i have next route:
route::get('users/search/{type?}/{value}', 'site\usercontroller@search');
the main thought simplify search:
if type exists (name, surname, email , etc.) search field. if not - search everywhere.
but when do:
http://example.com/users/search/sdgfdfxg
laravel throws
symfony \ component \ httpkernel \ exception \ notfoundhttpexception
with
http://example.com/users/search/name/sdgfdfxg
or
http://example.com/users/search/surname/sdgfdfxg
everything fine.
yust define 2 routes:
route::get('users/search/{type}/{value}',function($type,$value){ $app = app(); $controller = $app->make('site\usercontroller'); $controller->callaction($app, $app['router'], 'search', $parameters = array($type,$value)); }); route::get('users/search/{value}',function($value){ $app = app(); $controller = $app->make('site\usercontroller'); $controller->callaction($app, $app['router'], 'search', $parameters = array(null,$value)); });
but still easiest alternative alter order of defining you're parameters. set optional alternative end of url:
route::get('users/search/{value}/{type?}','site\usercontroller@search');
php laravel-4 laravel-routing
No comments:
Post a Comment