duanfen1312
2018-03-02 01:45
浏览 559
已采纳

如何在Laravel中设置动态路由前缀?

I need to create an app with multiple locales. And each route is prefixed with the locale. For example, xxx.com/en/home, xxx.com/fr/home.

The thing is, I need to dynamically bind the locale string to route prefix. Since users can change locale, the locale string is stored in session. And I need reference to the session on web.php. Session object can't be used in the globle scope on web.php, meaning session('key') won't get you anything (null) in the outermost scope except in route functions because Laravel parses web.php before instantiating any session object, I think. Therefore I face a conundrum in that I can't reference session in the outermost scope on web.php while I need session to create dynamic prefix. How can I solve this?

图片转代码服务由CSDN问答提供 功能建议

我需要创建一个包含多个区域设置的应用。 每个路由都以区域设置为前缀。 例如, xxx.com/en/home xxx.com/fr/home

事情是,我需要 动态绑定语言环境字符串到路由前缀。 由于用户可以更改区域设置,因此区域设置字符串存储在会话中。 我需要参考web.php上的会话。 Session对象不能在web.php的globle范围内使用,这意味着session('key')除了路由函数之外不会在最外层的范围内获取任何东西(null),因为Laravel在实例化任何会话之前解析web.php 对象,我想。 因此,我面临一个难题,即我不能在web.php的最外层范围内引用会话,而我需要会话来创建动态前缀。 我怎么解决这个问题?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongsu3664 2018-03-02 05:32
    已采纳

    I recently prefixed my routes with locale and I found it pretty easy to implement using mcamara's Laravel Localization package. After setting up the package installation, I just had to add a route group for all the URLs I wanted with locale prefix.

    Route::group([
        'prefix' => LaravelLocalization::setLocale(),
        'middleware' => ['localeSessionRedirect', 'localizationRedirect']
    ], function()
    {
       Route::get('/contact', 'HomeController@contact_page');
    });
    
    点赞 打赏 评论
  • ds78662302 2018-03-02 03:08

    I am not sure about your scenario and the complexity of you app, but I would try to keep things simple by generating all the routes at once something like

    $locales = [
        'en',
        'ru',
    ];
    
    foreach ($locales as $locale) {
        Route::group(['prefix' => $locale], function() {
            Route::get('route1',function(){});
            Route::post('route1',function(){});
        });
    } return false;
    

    and then I would write a middleware that parse the locale and set it accordingly. Hope it helps.

    点赞 打赏 评论
  • duanfei9278 2018-03-02 06:34

    You can use something like this:

    Route::prefix(App::getLocale())->middleware('lang')->group(function () { 
        // Routes
    });
    

    Lang middleware:

    class Language {
    
        public function handle(Request $request, Closure $next)
        {
            $locale = $request->segment(1);
    
            if (in_array($locale, config('app.locales'))) {
                \App::setLocale($locale);
                return $next($request);
            }
    
            if (!in_array($locale, config('app.locales'))) {
    
                $segments = $request->segments();
                $segments[0] = config('app.fallback_locale');
    
                return redirect(implode('/', $segments));
            }
        }
    
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题