connect( $route, $default = array ( ), $params = array ( ) )
Returns this object’s routes array. Returns false if there are no routes available.
$route | string | An empty string, or a route string “/” | required | (no default) |
$default | array | NULL or an array describing the default route | optional | array ( ) |
$params | array | An array matching the named elements in the route to regular expressions which that element should match. | optional | array ( ) |
Router::connect(‘/’, array(‘controller’ => ‘pages’, ‘action’ => ‘display’, ‘home’));
意思:在你访问siteurl/或者siteurl/index.php/时,controller为pages,action为display,view模板文件为/app/views/pages/home.ctp。
另外,connect函数还支持 模式匹配。如:
Router::connect(‘/pages/*’, array(‘controller’ => ‘pages’, ‘action’ => ‘display’));
意思:siteurl/pages/* 或者 siteurl/index.php/pages/*的任何网站都匹配为 ?controller=pages&action=display。
Router::parseExtensions( )
为了让你的路由处理不同类型的文件,你需要在你的路由配置文件再加上一行:
Router::parseExtensions(‘html’, ‘rss’, ‘xml’);
这将告诉路由去移除匹配的文件扩展名,然后在处理剩下的部分。
假如你想创建一个URL,比如/page/title-of-page.html, 你必须按以下说明的方式来创建你的路由:
Router::connect( '/page/:title', array('controller' => 'pages', 'action' => 'view'), array('pass' => array('title') ) );
然后如下就可以创建并访问指向该路由的链接。
以上来自官方CakePHP API以及Cook Book 的翻译。