路由内的Laravel app()-> getLocale()始终会打印默认的"en" [英] Laravel app()->getLocale() inside routes always print the default "en"

查看:433
本文介绍了路由内的Laravel app()-> getLocale()始终会打印默认的"en"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在路由中添加新的设置区域设置前缀时遇到问题. 当我从控制器或路由闭包中返回app()-> getLocale()时,它会正确返回新的设置语言环境,但是当我将其放在Route前缀中时,会得到默认的'en'.在我的情况下,新的语言环境为"ar".

I encountred a problem when trying to prefixing my routes with new set locale. When I return app()->getLocale() from a controller or a route closure it returns new set locale correctly, but when I put it inside Route prefix I get the default one 'en'. In my case the new locale is 'ar'.

这是我在web.php中的代码

This is my code inside web.php

<?php


//Request to put the choosen locale into the session
Route::get('locale/{locale}', function($locale) {

    session()->put('userLocale', $locale);
    return redirect(app()->getLocale());
});

//Group of routes that are supposed to be prefixed with new set locale 'ar'
Route::group(['prefix' => app()->getLocale(), 'middleware' => 'locale'], function() {

    Route::get('/', function() {
        return app()->getLocale();
    });
});

这是中间件:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;

class Locale
{
    protected $app;

    public function __construct(Application $app)
    {
        $this->app = $app;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $this->app->setLocale(session('userLocale'));
        return $next($request);
    }
}

请注意Route前缀内的语句"app()-> getLocale()",该语句的值始终为"en".但是,当我从闭包内部返回它时,它会打印新的设置语言环境"ar".

Note the statement "app()->getLocale()" inside Route prefix, this statement's value always is "en". But when I return it from within the closure it prints the new set locale "ar".

我要说的是,在前缀内部,app()-> getLocale()的值始终为"en",而在Closure中,其值为"ar".

因此,当我在浏览器中运行"localehost:8000/locale/ar"时,输出为"ar",这是正确的,但URL变为"localehost:8000/en",并且应为"localehost:8000/ar" 是吗?当我在浏览器中运行"localehost:8000/ar"时,我得到一个404页面.

So when I run "localehost:8000/locale/ar" in the browser the output is "ar" and that is correct but the url becomes "localehost:8000/en" and that should be "localehost:8000/ar" isn'it ? When I run "localehost:8000/ar" in the browser I get a 404 page.

我尝试从 RouteServiceProvider的mapWebRoutes方法中设置前缀,但是它不起作用,因为我认为服务提供商在中间件软件之前执行,因此它无法识别新的语言环境由Middlware设置.

I tried to set the prefix from within RouteServiceProvider's mapWebRoutes methode but It doesn't work because I think that the service provider is executed before the middlware so It can't recognize the new locale that is set by the middlware.

我希望获得帮助或使用新的语言环境为路由添加前缀的另一种方法,因为也许我做错了什么.

推荐答案

在您的中间件运行之前,不会从会话中设置您的语言环境.您的路由必须在中间件运行之前定义,否则,系统将不知道哪个中间件应该在哪个路由上运行.

Your locale is not going to be set from the session until your middleware runs. Your routes would have to be defined before the middleware runs, otherwise, the system would have no idea which middleware are supposed to be run on which routes.

由于app()-> locale()在中间件运行之前将返回"en",因此您所做的所有代码都定义了"en"的前缀.

Since app()->locale() is going to return 'en' before your middleware runs, all your code is doing is defining a prefix of 'en'.

似乎您想为语言环境定义一个变量,在这种情况下,您将执行以下操作:

It seems maybe you want to define a variable for the locale, in that case, you would do something like:

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

或者您可以使用一个为您处理本地化网址的软件包,例如 https://github.com/mcamara/laravel-localization

Or you could use a package that handles localized urls for you like https://github.com/mcamara/laravel-localization

这篇关于路由内的Laravel app()-&gt; getLocale()始终会打印默认的"en"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆