如何在带有自定义 URL 和扩展的 cakephp 中使用国际化
How to use internationalization in cakephp with custom URL and extensions
我正在为应用程序使用 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-]*'));
此处用于 "about-us"、'contact-us',第三个也是主要的是“:slug”(动态)
两个问题:
- 当我尝试更改语言时,url 显示
lang/controller/action
( abc.com/eng/homes/about_us
) 而不是 abc.com/eng/about-us.html
或 abc.com/chi/about-us.html
- 对于其他动态 url 例如。
abc.com/page-1.html
、abc.com/page-2.html
、abc.com/page-3.html
等等...如何将 abc.com/lang/slug
与 .html
(abc.com/eng/page-1.html
或 abc.com/chi/page-1.html
)一起使用
为此我使用
Router::connect('/:slug', array('controller' => 'homes', 'action' => 'page'), array('pass' => array('slug'), 'slug'=>'[a-zA-Z0-9-]*'))
规则。
When i try to change language, url is showing lang/controller/action ( abc.com/eng/homes/about_us )
问题中的路由定义首先是这样的:
Router::connect(
'/:language/:controller/:action/*',
array(),
array('language' => 'eng|chi')
);
路由按照声明的顺序进行测试 - 此路由将匹配任何传递给它的语言 eng
或 chi
的 url,因为所有 urls 有一个控制器和一个动作,这条路线将始终匹配。
instead of abc.com/eng/about-us.html or abc.com/chi/about-us.html
如果 /:lang/:slug
需要路由匹配 - 则必须有匹配该模式的路由定义 - 目前没有。
for other dynamic url eg abc.com/page-1.html, abc.com/page-2.html, abc.com/page-3.html so on ... how to use abc.com/lang/slug with .html ( abc.com/eng/page-1.html or abc.com/chi/page-1.html)
我不完全理解这个问题,但答案可能是确保所有路由都带有前缀,并且在没有前缀的路由之前定义。例如:
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 可以提供帮助。
我正在为应用程序使用 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-]*'));
此处用于 "about-us"、'contact-us',第三个也是主要的是“:slug”(动态)
两个问题:
- 当我尝试更改语言时,url 显示
lang/controller/action
(abc.com/eng/homes/about_us
) 而不是abc.com/eng/about-us.html
或abc.com/chi/about-us.html
- 对于其他动态 url 例如。
abc.com/page-1.html
、abc.com/page-2.html
、abc.com/page-3.html
等等...如何将abc.com/lang/slug
与.html
(abc.com/eng/page-1.html
或abc.com/chi/page-1.html
)一起使用
为此我使用
Router::connect('/:slug', array('controller' => 'homes', 'action' => 'page'), array('pass' => array('slug'), 'slug'=>'[a-zA-Z0-9-]*'))
规则。
When i try to change language, url is showing lang/controller/action ( abc.com/eng/homes/about_us )
问题中的路由定义首先是这样的:
Router::connect(
'/:language/:controller/:action/*',
array(),
array('language' => 'eng|chi')
);
路由按照声明的顺序进行测试 - 此路由将匹配任何传递给它的语言 eng
或 chi
的 url,因为所有 urls 有一个控制器和一个动作,这条路线将始终匹配。
instead of abc.com/eng/about-us.html or abc.com/chi/about-us.html
如果 /:lang/:slug
需要路由匹配 - 则必须有匹配该模式的路由定义 - 目前没有。
for other dynamic url eg abc.com/page-1.html, abc.com/page-2.html, abc.com/page-3.html so on ... how to use abc.com/lang/slug with .html ( abc.com/eng/page-1.html or abc.com/chi/page-1.html)
我不完全理解这个问题,但答案可能是确保所有路由都带有前缀,并且在没有前缀的路由之前定义。例如:
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 可以提供帮助。