CakePHP 4 -使用i18 n语言参数进行管理前缀路由

bis0qfac  于 2022-11-11  发布在  PHP
关注(0)|答案(2)|浏览(184)

我有cakephp4.2项目,我已经实现了多语言方法,其中当前语言是sept在会话中。URL是相同的所有语言。
我也想在网址中添加语言参数。我在config/routes.php文件中尝试了下面的代码,它是正常的(非管理员前缀)网址工作。但不工作的Admin前缀的网址。

use Cake\Routing\RouterBuilder;

$routes->setRouteClass(DashedRoute::class);

$routes->scope('/', function (RouteBuilder $builder) {
    $builder->connect('/', ['controller' => 'Clients', 'action' => 'index']);

    $builder->connect('/:language/:controller/:action/*', array(), array('language' => 'en|ar'));
    $builder->connect('/:language/:controller', array('action' => 'index'), array('language' => 'en|ar'));
    $builder->connect('/:language', array('controller' => 'Clients', 'action' => 'index'), array('language' => 'en|ar')) ;

    $builder->fallbacks();
});

$routes->prefix('Admin', function (RouteBuilder $routes) {
    $routes->connect('/', ['controller' => 'Users', 'action' => 'login']);
    //added below line for language but it is not working
    //$routes->connect('/:language', array('controller' => 'Users', 'action' => 'login'), array('language' => 'en|ar')) ;
    $routes->fallbacks(DashedRoute::class);
});

工作url

localhost/cake4-lng/     
localhost/cake4-lng/en    
localhost/cake4-lng/ar    
localhost/cake4-lng/en/controller    
localhost/cake4-lng/admin/users/dashboard /*notice there is no language param with Admin*/

非工作url

localhost/cake4-lng/en/admin/users/dashboard    
localhost/cake4-lng/ar/admin/users/dashboard

以上2个URL上的错误为:
找不到AdminController。

gkn4icbw

gkn4icbw1#

首先,无论何时遇到路由问题,请检查bin/cake routes,以查看哪些路由是以哪种方式连接的。
也就是说,你是用前缀中的:language元素来连接路由的,所以你的URL不能是/en/admin/...,而只能是/admin/en/...。为了实现前者,你必须把前缀 Package 在一个带有相应语言元素的作用域中,大致如下:

$routes->scope('/:language', function (RouteBuilder $routes) {
    $routes->prefix('Admin', function (RouteBuilder $routes) {
        $patterns = ['language' => 'en|ar'];

        $routes
            ->connect('/:controller/:action/*', [])
            ->setPatterns($patterns);
        $routes
            ->connect('/:controller', ['action' => 'index'])
            ->setPatterns($patterns);
        $routes
            ->connect('/', ['controller' => 'Users', 'action' => 'login'])
            ->setPatterns($patterns);
    });
});

这将导致像/:language/admin/...这样的模板被连接起来。

v1uwarro

v1uwarro2#

部分完成
URL现在使用语言参数,但链接不使用HTML帮助器使用语言参数填充。
echo $this->Html->link('test',['controller'=>'clients','action'=>'index']);
生成如下html,不带语言参数
<a href="/clients/index">test</a>
而我需要<a href="/en/clients/index">test</a><a href="/ar/clients/index">test</a>
我在routes.php中添加了以下代码用于管理路由

$routes->prefix('Admin', function (RouteBuilder $routes) {
    $routes->connect('/:language', array('controller' => 'Users', 'action' => 'login'), array('language' => 'en|ar')) ;
    $routes->connect('/:language/:controller', array('action' => 'index'), array('language' => 'en|ar'));
    $routes->connect('/:language/:controller/:action/*', array(), array('language' => 'en|ar'));

    $routes->connect('/', ['controller' => 'Users', 'action' => 'login']);
    //$routes->connect('/:language', array('controller' => 'Users', 'action' => 'login'), array('language' => 'en|ar')) ;
    $routes->fallbacks(DashedRoute::class);
});

以及appController.php中的文件

public function beforeFilter(EventInterface $event){       
    parent::beforeFilter($event); 
    $Session = $this->request->getSession(); 
    $urlLang = $this->request->getParam('language');
    if($urlLang!=null && in_array(strtolower($urlLang),['en','ar'])){
        $lng_val = strtolower($urlLang)=='ar'?'ar_AE':'en_us';
        $Session->write('Config.language',$lng_val);
    } 
    if($Session->check('Config.language')){
        I18n::setLocale($Session->read('Config.language'));
    }else{
        $Session->write('Config.language','ar_AE');
    } 
}

public function changeLanguage($language=null){
    $Session = $this->request->getSession(); 
    if($language!=null && in_array($language,['en_US','ar_AE'])){
        $Session->write('Config.language',$language);
        return $this->redirect($this->referer());
    }
    else
    {
        $Session->write('Config.language',I18n::locale());
        return $this->redirect($this->referer());
    }
}

相关问题