I am creating a site with CakePHP, and I need to set some URLs for static pages, which are handled by the pages controller. Basically I want to have two different types of static pages, with the URLS
mysyte.com/page
which should map to app/views/pages/page.ctp
and
mysite.com/special/page
which should map to app/views/pages/special-page.ctp
. N开发者_如何学运维ote that in the first case page can be 'special' as well.
I am a bit lost with the routing I have to set up for this situation. I have tried to use the two routes
Router::connect(
'/special/:mypage',
array('controller' => 'pages', 'action' => 'display'),
array('pass' => array('mypage'), 'mypage' => '[a-z]+')
);
Router::connect('/*', array('controller' => 'pages', 'action' => 'display'));
and in the pages controller
function display($page = null, $mypage = null) {
if ($mypage) {
$path = array('special-'. $mypage);
}
else {
$path = func_get_args();
}
//The rest of the display action
}
The problem is that is seems that :mypage
is passed as the first parameter in the action, which is page
, and not as the mypage
parameter.
How can I possibly fix this?
You're passing only one parameter to the action via routing, that's why it's the first one - its the only one. Names don't really matter.
I would do it like this:
Router::connect(
'/:page',
array('controller' => 'pages', 'action' => 'display'),
array('pass' => array('page'), 'page' => '[a-z]+')
);
Router::connect(
'/special/:mypage',
array('controller' => 'pages', 'action' => 'display_special'),
array('pass' => array('page'), 'page' => '[a-z]+')
);
Controller code:
function display($page) {}
function display_special($page) {}
But if you want your way, try this:
Router::connect(
'/:special/:mypage',
array('controller' => 'pages', 'action' => 'display'),
array('pass' => array('special', 'mypage'), 'mypage' => '[a-z]+')
);
Actually I have discovered that the pages controller already handles this situation, checking for page and subpage. The URL mysite.com/special/mypage
points to app/views/pages/special/mypage.ctp
with the only rule
Router::connect('/*', array('controller' => 'pages', 'action' => 'display'));
精彩评论