Laravel路由where子句无法按预期工作

时间:2018-08-27 09:22:09

标签: laravel laravel-5

我有以下两条路线都可以正常工作:

Route::get('/de', 'FrontpageController@index');

Route::get('/fr', 'FrontpageController@index');

计划是将它们合并为一条语句,如以下显然可行的解决方案中所述:https://stackoverflow.com/a/34404404/4688612

所以新代码如下:

Route::get('/{url}', 'FrontpageController@index')->where('url', 'de|fr');

但是,我得到了This page isn’t working. example.test redirected you too many times.

我什么都不来?

索引方法如下:

public function index(Request $request){

        $geoIpRecord = getGeoIpRecord();

        $nearest_places = getNearestPlaces( $geoIpRecord->location->latitude, $geoIpRecord->location->longitude );

        if($request->path() == 'de'){

            $page_title = 'Titel';

            $phone = constant('default_phone_de'); 

            return view('partials.main', compact('nearest_places', 'page_title', 'phone'));

        } elseif ($request->path() == 'fr') {

            $page_title = 'Titre';

            $phone = constant('default_phone_fr'); 

            return view('fr.partials.main', compact('nearest_places', 'page_title', 'phone'));
        }
    }

修改

同时,我发现我正在使用的本地化软件包在某种程度上干扰了/{url}参数。

在我的web.php中使用以下代码时,/{url}参数停止工作。它只是变得无聊。

Route::group(
[
    'prefix' => LaravelLocalization::setLocale(),
    'middleware' => [ 'localize', 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
],
function()
{
    Route::get('/', function()
    {
        return redirect(LaravelLocalization::getCurrentLocale());
    });
});

我个人认为这是一个错误,因此已经联系了该localizaiton软件包的开发人员。

1 个答案:

答案 0 :(得分:0)

如果您使用的是https://github.com/mcamara/laravel-localization,那么我认为您必须像这样定义路线

所有受多语言支持的路线都应在该路线组内

Route::group(['prefix' => LaravelLocalization::setLocale()], function()
{
    /** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/
    Route::get('/', function()
    {
        return View::make('hello');
    });

    Route::get('test',function(){
        return View::make('test');
    });
});

此处匹配example.com/de/example.com/de/test

然后定义该路由组之外的所有不受语言支持的路由

相关问题