Laravel路线,其中where子句不按预期工作

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

我有以下路线,两者都很好:

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包的开发人员。

laravel laravel-5
1个回答
0
投票

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

所有支持多语言的路由都应该在此Route组中

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

然后定义此Route组外的所有非语言支持的路由

© www.soinside.com 2019 - 2024. All rights reserved.