dregvw1801 2017-01-26 21:52
浏览 230
已采纳

如何从Laravel中的路由操作中删除路由前缀

I am creating localization in Laravel 5.3 application. It must follow such requirements:

  1. If route path contains locale prefix, locale should be set according to this prefix

  2. If there's no route prefix, it should be set to English.

For instance:

A) mysite.org/pl/blog/133 sets locale to pl and calls BlogController with parameter 113

B) mysite.org/en/blog/133 sets locale to en and calls BlogController with parameter 113

C) mysite.org/blog/133 acts in the same way as previous.

To implement this, I was using the following code:

#routes/web.php

require base_path('routes/common.php');
// TODO set default locale to en

Route::group(['prefix' => '{locale}'], function($group) {

    require base_path('routes/common.php');
    // TODO set locale to $locale

    foreach ($group -> getRoutes() as $route) {
        $route->where('locale', 'en|ru|pl|ua|de');
    }

});


#routes/common.php

Route::get('/blog/{id}', function($id) {
    // Return view of respective blogpost
});

The problem is that when I use path A or B i get an extra first parameter in my action function, which derives from the locale prefix.

Can I hide this parameter somehow? Or is there a way to achieve what I want in a way more architecturally correct?

  • 写回答

1条回答 默认 最新

  • dongyi7041 2017-01-27 05:49
    关注

    I'm not sure my solution is the best or not but you can try this

    1. create new file in app/Http/Middileware/Language.php

    ...

    namespace App\Http\Middleware;
    
    use Closure;
    use Illuminate\Routing\Redirector;
    use Illuminate\Http\Request;
    
    class Language {
    
        public function __construct(Redirector $redirector) {
            // $this->app = $app;
            $this->redirector = $redirector;
            // $this->request = $request;
        }
    
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
        public function handle($request, Closure $next)
        {
            // Make sure current locale exists.
            $segments = $request->segments();
            $locale = $segments[0];
    
            $languages = ['en','ru','pl','ua','de'];
            if (!in_array($locale, $languages)) {
                $segments = array_merge(['en'], $segments);
                return $this->redirector->to(implode('/', $segments));
            }
    
            return $next($request);
        }
    
    }
    
    1. In app/Http/Kernel.php and this line

      protected $middleware = [ \App\Http\Middleware\Language::class, \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,

      ];

    2. In file route (route/web.php) add prefix in group

      Route::group(['middleware' => ['web'], 'prefix' => '{locale}'], function () {

    .....

    Hope this help

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据