A URL like /blog/history/05/june can then be handled like this:
Route Handling in a Controller
<?php
class BlogController extends AppController
{
function history($year, $month=null)
{
// .. Display appropriate content
}
}
?>
Route Example<?php$Route->connect ('/blog/:action/*', array('controller'=>'Blog', 'action'=>'index'));?>A URL like /blog/history/05/june can then be handled like this:Route Handling in a Controller<?phpclass BlogController extends AppController{ function history($year, $month=null) { // .. Display appropriate content }}?>
URL中的'history'通过Blog's route中的:action匹配。URL中通过*匹配的内容都被当作parameters传递到:action对应的方法中,就像这里的$year和$month。如果URL是/blog/history/05,则history()方法只会得到一个参数,05。
下面这个例子是CakePHP默认设置的一个route来为PagesController::display('home')配置路由。Home是Cake的默认首页视图,你可以在这个位置找到并修改它/app/views/pages/home.thtml。
* The define below is used to turn cake built webservices
* on or off. Default setting is off.
*/
define('WEBSERVICES', 'on');
/app/config/core.php (partial)/** * The define below is used to turn cake built webservices * on or off. Default setting is off. */ define('WEBSERVICES', 'on');
然后我在controller中构建代码如下:
<?php
class PhonesController extends AppController
{
function doWhosOnline()
{
// this action is where we do all the work of seeing who's on the phone...
// If I wanted this action to be available via Cake's xml webservices route,
// I'd need to include a view at /app/views/posts/xml/do_whos_online.thtml.
// Note: the default view used here is at /app/views/layouts/xml/default.thtml.
// If a user requests /phones/doWhosOnline, they will get an HTML version.
// If a user requests /xml/phones/doWhosOnline, they will get the XML version.
}
}
?>
<?phpclass PhonesController extends AppController{ function doWhosOnline() { // this action is where we do all the work of seeing who's on the phone... // If I wanted this action to be available via Cake's xml webservices route, // I'd need to include a view at /app/views/posts/xml/do_whos_online.thtml. // Note: the default view used here is at /app/views/layouts/xml/default.thtml. // If a user requests /phones/doWhosOnline, they will get an HTML version. // If a user requests /xml/phones/doWhosOnline, they will get the XML version. }}?>
Section 5 自定义反射配置(可选)Cake的命名约束真的是非常不错,你可以命名model为Box,controller为Boxes,于是所有的问题都解决了。但是考虑到有的情况下(特别是非英语国家)这样的命名约束不如你意的话,你可以通过自定义反射配置来满足你的需求。
/app/config/inflections.php,这里定义了Cake的一些变量,你可以来调整名字单复数约定等。你需要详细阅读文件中的注释,并且最好具备正则表达式知识,否则请误随便修改该配置文件。