Cakephp 3路由与语言参数 [英] Cakephp 3 routing with language parameter

查看:184
本文介绍了Cakephp 3路由与语言参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将cakephp 2.x转换为3.x.我使用 Router :: connect()规则,但我尝试将它们转换为范围版本。

I'm trying to convert cakephp 2.x to 3.x. I was using Router::connect() rules, but I try to convert them to scope version.

to myold routing rule,in config / routes.php 我添加了这个。

Regarding to myold routing rule, in config/routes.php I added this.

  Router::defaultRouteClass('Route');
  Router::scope('/', function ($routes) {

    $routes->connect('/:language/:controller/:action/*', ['language' => 'ar|de|en|fr']);
    $routes->connect('/:language/:controller', ['action' => 'index', 'language' => 'ar|de|en|fr']);
    $routes->connect('/:language', ['controller' => 'Mydefault', 'action' => 'index', 'language' => 'ar|de|en|fr']);

    $routes->redirect('/gohere/*', ['controller' => 'Mycontroller', 'action' => 'myaction'], ['persist' => array('username')]);

    $routes->connect('/', ['controller' => 'Mydefault', 'action' => 'index']);

    $routes->fallbacks('InflectedRoute');
});




  • 但在 example.com/ en / works 。我得到这个错误:错误:无法找到worksController。因为我的控制器文件 WorksController.php li>

    • But this fails in example.com/en/works. I get this error: Error: worksController could not be found. Because my controller file is WorksController.php.
    • 控制器名称部分挂在句子cakephp 3上? http://book.cakephp.org/3.0/en/intro/conventions.html#控制器惯例

      Does controller name part hanged to sentence casein cakephp 3 ? http://book.cakephp.org/3.0/en/intro/conventions.html#controller-conventions


      • example.com/foo/bar 给出此错误:错误:找不到barController。。但 foo 是控制器, bar 是动作。

      • Also example.com/foo/bar gives this error: Error: barController could not be found.. But foo is controller and bar is action.

      如何解决此路由问题?

      编辑:

      更改 defaultRouteClass('Route') Route :: defaultRouteClass('InflectedRoute')解决问题1.但问题2存在。


      Changing Route::defaultRouteClass('Route') to Route::defaultRouteClass('InflectedRoute') solved problem 1. But problem 2 exists.

      推荐答案

      选项,如路由元素模式,必须通过 Router :: connect() code>, $ options 参数。

      Options, such as route element patterns, must be passed via the third argument of Router::connect(), the $options argument.

      此路线

      $routes->connect('/:language/:controller', ['action' => 'index', 'language' => 'ar|de|en|fr']);
      

      会捕获您的 / foo / bar URL ,它会为:language 元素和 bar foo

      will catch your /foo/bar URL, it will match foo for the :language element, and bar for the :controller element.

      定义路线的正确方法是

      $routes->connect(
          '/:language/:controller',
          ['action' => 'index'],
          ['language' => 'ar|de|en|fr']
      );
      

      其他路线需要相应调整。

      The other routes need to be adapted accordingly.

      另请参阅 食谱>路由>连接路由

      See also Cookbook > Routing > Connecting Routes

      这篇关于Cakephp 3路由与语言参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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