前缀和中间件由于某种原因不起作用。对于一些路线

时间:2018-05-14 21:59:09

标签: php laravel

所以我试图把我的本地化作为我的网站域名的前缀,所以我做了一个中间件,把你的本地化lang放在网址上,你试图打开一个页面继承我的中间件

 public function handle($request, Closure $next)
{
    if ($request->method() === 'GET') {
        $segment = $request->segment(1);


        if (!in_array($segment, config('app.locales'))) {
            $segments = $request->segments();
            $fallback = session('locale') ?: config('app.fallback_locale');
            $segments = array_prepend($segments, $fallback);

            return redirect()->to(implode('/', $segments));
        }

        session(['locale' => $segment]);
        app()->setLocale($segment);
    }
    return $next($request);
}

我将中间件添加到了routemiddleware

protected $routeMiddleware = [
    'Locate' =>  \App\Http\Middleware\Locale::class,
];

我确实为我的所有路线添加了前缀和中间件

Route::prefix('{lang?}')->middleware('Locate')->group(function() {

Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout')->name('logout');

Route::get('/home', 'HomeController@index')->name('home');


Route::get('/admin/login', function () {
    return view('admin.auth.login');
})->name('AdminLogin');


Route::get('/contact-us', 'ContactUsController@Contactus')->name('ContactUs');
Route::post('/contact-us', 'ContactUsController@PostContactus')->name('PostContactUs');




Route::prefix('auth')->group(function () {
    Route::get('/login', function () {
        return view('auth.login');
    })->name('Login');

    Route::post('/login', 'Auth\LoginController@Login')->name('userslogin');

});



Route::prefix('search')->group(function () {
    Route::get('/categories', 'search\SearchController@Categories')->name('Categories');
    Route::get('/filter/{categoryseo}', 'search\SearchController@filter')->name('InstructorsSearch');
    //sending categoryseo to the filter page so i can put it in hidden input in the filter page and use it to get the list

    Route::get('/list', 'search\SearchController@InstructorList')->name('InstructorsSearchList');
    Route::get('/profile/{userid?}', 'search\SearchController@instructorprofile')->name('InstructorProfile');
});

});

由于某些原因,像/ home这样的页面获取中间件并像我想要的那样改变/ en / home,对于其他类似的搜索/类别,它甚至不会注意到我的中间件,但我试图删除本地化的前缀只是把我的中间件工作,它注意到我的中间件。使用laravel 5.5

0 个答案:

没有答案