Laravel 5 making a route for a specific link

谁说我不能喝 提交于 2019-12-02 17:06:00

问题


I have a link that goes to a address like

http://localhost/pages/vehicles?show=61

Is it possible to make a route for that kind of link i tired below route but it does not work

Route::get('/pages/vehicles?show={id}', ['middleware' => ['roles'], 'uses' => 'PagesController@show', 'roles' => ['Admin']]);

回答1:


I had a similar problem trying to send a form directly to a route (using something like /pages/vehicles/61) but it seems to be imposible (question here).

If you don't have a specific route for all vehicles (/pages/vehicles doesn't show a list of vehicles) you can do something like:

Route::get('pages/vehicles','PagesControler@show');

And inside your controller

$show = Request::input('show');

and then whatever you need to do with that. Otherwise, a "hack" like I did or javascript (I'm assuming you are sending this from a form).




回答2:


Not need to put query parameters in the route. So please try without that part.

Route::get('/pages/vehicles', ['middleware' => ['roles'], 'uses' => 'PagesController@show', 'roles' => ['Admin']]);

You can get the query parameter, in your controller method like below and do the processing.

$id = Request::input('show');

You can have any number of query parameters in the request url. But doesn't need to define them inside the route path. But if you want the query parameter (id as in your case) as a part of the request url, the you have to define it in the route path. For example if your url want to be something like http://localhost/pages/vehicles/61, then your route configuration should look like below.

Route::get('/pages/vehicles/{id}', ['middleware' => ['roles'], 'uses' => 'PagesController@show', 'roles' => ['Admin']]);


来源:https://stackoverflow.com/questions/30912744/laravel-5-making-a-route-for-a-specific-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!