Php laravel中具有相同参数数的多条路由

Php laravel中具有相同参数数的多条路由,php,laravel,Php,Laravel,我的客户希望在网站中有两个URL: a) www.example.com/{cityname}-items.html b) www.example.com/{statename}-items.html 如何为这两个URL编码两条路由?第一个列表项目位于一个城市,第二个列表项目位于一个州 Route::get('{city}', 'CityController@searchCity'); Route::get('{state}', 'CityController@searchState');

我的客户希望在网站中有两个URL:

a) www.example.com/{cityname}-items.html 
b) www.example.com/{statename}-items.html
如何为这两个URL编码两条路由?第一个列表项目位于一个城市,第二个列表项目位于一个州

Route::get('{city}', 'CityController@searchCity');
Route::get('{state}', 'CityController@searchState');

我什么时候做这个?只有第一个路由可以工作,因为两个路由都有相同数量的参数。客户端希望在不更新url的情况下执行此操作:基本上:您不能。例如,当你点击时,你的系统没有办法找到它

www.example.com/whashington-items.html
如果你想去whashington州whashington市

你应该考虑拆分你的逻辑:

www.example.com/city/{name}-items.html
www.example.com/state/{name}-items.html
2“端点”(或某种程度上),我认为这听起来更好。

请尝试以下方法:

Route::get('{cityOrState}-items.html', function($cityOrState){
    $city = City::find($cityOrState);

    if(is_null($city))
    {
        return App::make('CityController')->searchState($cityOrState);
    }

    return App::make('CityController')->searchCity($cityOrState);
});

你不能那样做。bcs当您通过url到达时,路线与城市和州没有任何区别。要解决这个问题,至少您必须放置一些键,如“/c/{city}-items.html”和“s/{state}-items.html”来区分它们。