如何在cakephp中使用带有自定义网址和扩展程序的国际化

时间:2017-09-05 14:17:34

标签: cakephp routing cakephp-2.0

我正在使用cakephp 2.0进行申请...
一切都很好,但我对自定义URL或SEO友好URL有一些问题。

以下是我正在使用的路线规则

Router::parseExtensions('html', 'rss', 'xml','json');
    Router::connect('/:language/:controller/:action/*',array(),array('language' => 'eng|chi'));
    Router::connect('/', array('controller' => 'homes', 'action' => 'index'),array('language' => 'eng|chi'));       
    Router::connect('/backend/*', array('controller' => 'users', 'action' => 'login', 'lab' => true));
    Router::connect('/about-us/*', array('controller' => 'homes', 'action' => 'about_us'));
    Router::connect('/contact/*', array('controller' => 'homes', 'action' => 'contact'));
    Router::connect('/:slug', array('controller' => 'homes', 'action' => 'page'), array('pass' => array('slug'), 'slug'=>'[a-zA-Z0-9-]*'));

此处为"关于我们",'联系我们'第三和主要的是":slug" (动态)

两个问题:

  1. 当我尝试更改语言时,网址会显示lang/controller/actionabc.com/eng/homes/about_us)而不是abc.com/eng/about-us.htmlabc.com/chi/about-us.html
  2. 用于其他动态网址,例如。 abc.com/page-1.htmlabc.com/page-2.htmlabc.com/page-3.html等......如何将abc.com/lang/slug.htmlabc.com/eng/page-1.htmlabc.com/chi/page-1.html)一起使用< / LI>

    为此我正在使用

    Router::connect('/:slug', array('controller' => 'homes', 'action' => 'page'), array('pass' => array('slug'), 'slug'=>'[a-zA-Z0-9-]*')) 
    

    规则。

1 个答案:

答案 0 :(得分:1)

  

当我尝试更改语言时,url显示lang / controller / action(abc.com/eng/homes/about_us)

问题中的路线定义首先包含:

Router::connect(
    '/:language/:controller/:action/*',
    array(),
    array('language' => 'eng|chi')
);

路由按照声明的顺序进行测试 - 此路由将匹配任何传递给它的语言engchi的网址,因为所有网址都有控制器和此路由将执行的操作永远匹配。

  

而不是abc.com/eng/about-us.html或abc.com/chi/about-us.html

如果需要有/:lang/:slug的路线匹配 - 那么必须有一个与该模式匹配的路线 - 目前还没有。

  

其他动态网址,例如abc.com/page-1.html,abc.com/page-2.html,abc.com/page-3.html等...如何使用abc.com/lang / slug with .html(abc.com/eng/page-1.html or abc.com/chi/page-1.html)

我不完全理解这个问题,但答案可能是确保所有路由都有前缀,并且在没有前缀的路由之前定义。 e.g:

Router::parseExtensions('html', 'rss', 'xml','json');

// Define Chinese language routes first as they have a prefix
Router::connect('/chi/', ['controller' => 'homes', 'action' => 'index'],['language' => 'chi']);
Router::connect('/chi/backend/*', ['controller' => 'users', 'action' => 'login', 'lab' => true],['language' => 'chi']);
Router::connect('/chi/about-us/*', ['controller' => 'homes', 'action' => 'about_us'],['language' => 'chi']);
Router::connect('/chi/contact/*', ['controller' => 'homes', 'action' => 'contact'],['language' => 'chi']);
Router::connect('/chi/:slug', ['controller' => 'homes', 'action' => 'page'], ['pass' => ['slug'], 'slug'=>'[a-zA-Z0-9-]*'],['language' => 'chi']);
Router::connect('/chi/:controller/:action/*',[],['language' => 'chi']);

// Define English language routes last as they don't have a prefix
Router::connect('/', ['controller' => 'homes', 'action' => 'index'],['language' => 'eng']);
Router::connect('/backend/*', ['controller' => 'users', 'action' => 'login', 'lab' => true],['language' => 'eng']);
Router::connect('/about-us/*', ['controller' => 'homes', 'action' => 'about_us'],['language' => 'eng']);
Router::connect('/contact/*', ['controller' => 'homes', 'action' => 'contact'],['language' => 'eng']);
Router::connect('/:slug', ['controller' => 'homes', 'action' => 'page'], ['pass' => ['slug'], 'slug'=>'[a-zA-Z0-9-]*'],['language' => 'eng']);
Router::connect('/:controller/:action/*',[],['language' => 'eng']);

通过这种方式,所有路由都可以明确地解析,并且也可以明确地生成 - 假设在生成URL时总是传递语言; persist parameter可以帮助解决这个问题。