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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?