从您提到的页面收集代码:
约束:您不能在此设置中使用名为 tips 或 foo 的控制器
在/config/routes.php:
$subdomain = substr( env("HTTP_HOST"), 0, strpos(env("HTTP_HOST"), ".") );
if( strlen($subdomain)>0 && $subdomain != "m" ) {
Router::connect('/tips',array('controller'=>'mobiles','action'=>'tips'));
Router::connect('/foo', array('controller'=>'mobiles','action'=>'foo'));
Configure::write('Site.type', 'mobile');
}
/* The following is available via default routes '/{:controller}/{:action}'*/
// Router::connect('/mobiles/tips',
// array('controller' => 'mobiles', 'action'=>'tips'));
// Router::connect('/mobiles/foo',
// array('controller' => 'mobiles', 'action'=>'foo'));
在您的控制器操作中:
$site_is_mobile = Configure::read('Site.type') ?: '';
那么在你看来:
<?php
if ( $site_is_mobile ) {
// $html will take care of the 'm.example.com' part
$html->link('Cool Tips', '/tips');
$html->link('Hot Foo', '/foo');
} else {
// $html will just output 'www.example.com' in this case
$html->link('Cool Tips', '/mobiles/tips');
$html->link('Hot Foo', '/mobiles/foo');
}
?>
这将允许您在视图中输出正确的链接(稍后我将向您展示如何编写更少的代码)但 $html 助手将无法 - 无论如何魔法 -使用控制器动作路由到另一个域。请注意,就 $html 帮助器而言,m.example.com 和 www.example.com 是不同的域。
现在,如果您愿意,您可以在控制器中执行以下操作,以消除视图中的一些逻辑:
<?php
$site_is_mobile = Configure::read('Site.type') ?: '';
if ( $site_is_mobile !== '' ) {
$tips_url = '/tips';
$foo_url = '/foo';
} else {
$tips_url = '/mobile/tips';
$foo_url = '/mobile/foo';
}
// make "urls" available to the View
$this->set($tips_url);
$this->set($foo_url);
?>
在您看来,您无需担心是否通过m.example.com/tips 或www.example.com/mobile/tips 访问该站点:
<?php echo $html->link("Get some kewl tips", $tips_url); ?>
CakePHP-1.3 中更高级的路由参考Mark Story's article on custom Route classes
让我们知道 ;)