如何在laravel中制作多条路线

问题描述 投票:0回答:1

我有这两条路线

 Route::get('/{bank}', array('as' => 'id', 'uses' => 'BanksController@single'));
Route::get('/{city?}/{sort?}', 'BanksController@index');

当我输入http / alfa_bank时,我看到BanksController @ single动作当我输入/ new_york我看到同样的

如何在条件下看到BanksController @ index动作,而不是它在路由器中的第一个参数

laravel routes
1个回答
3
投票

由于银行和城市共享相同的字符集,您实际上无法区分这两者,而Laravel的路由不允许您从另一条路线中进入另一条路线。

看起来索引路线应该是特定城市中的所有银行(如果提供了城市),所以我建议以下路线定义:

Route::get('/', 'BanksController@index');
Route::get('/{bank}', ['as' => 'id', 'uses' => 'BanksController@single']);

在索引方法中,您仍然可以查找城市并将类型排序为查询参数:

$city = $request->query('city');
$sort = $request->query('sort');

索引的URL如下所示:

https://example.com/?city=new_york
https://example.com/?city=new_york&sort=asc
© www.soinside.com 2019 - 2024. All rights reserved.