I'm try to create search engine friendly URLs for the pages controller, i.e. /about
instead of /pages/about
.
I've tried setting up the following routes (at the bottom of routes.php):
Router::connect('/*', array('controller' => 'pages', 'action' => 'display'));
and
Router::connect('/:page', array('controller' => 'pages',
'action' => 'display'), array('pass' => array('page'), 'page' => '[a-z]+'));
Both properly match /about
, /support
, etc. However, failed when I had a action/method pair. For example, /contact
should route to PagesController->contact()
. However, the above routed it to PagesController->display()
.
There has to be a way to accomplish this without making a specific route for each page. How can I create a route or set of routes that:
- Mimics the default route behavior for the PagesController. That is routes to display() unless a action/method pair exists.
- Does so with search engine friendly URL. That is coming from root
/
not/pages
. - Demonstrate both the
Router::connect()
andHtml->link()
I have checked for examples in the CakePHP Book and viewed other questions such as CakePHP routing in pages controller. Nothing seems to satisfy the specificatio开发者_StackOverflown above.
You need to create a second route for the contact method call and put it before the more generic rule to match "everything [a-z] after /pages". Try that before your rule:
Router::connect('/contact', array('controller' => 'pages', 'action' => 'contact'));
Always keep in mind that the order of routes is important. The more generic a rule is the more will it match. So put more specific rules in front of the more generic ones.
精彩评论