Laravel 5 making a route for a specific link

2020-05-02 10:09发布

问题:

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']]);