Taking Advantage of the Pages Controller
Looking for a few tips on how to make the Pages Controller your new friend? Out-of-the-box it automatically maps incoming requests to their views, sets the page title, and even handles sub-pages well. But wait, there's more!
Looking for a few tips on how to make the Pages Controller your new friend? Out-of-the-box it maps incoming requests to their views, sets the page title, and even handles sub-pages well. But wait, there's more!
Of course, we could manipulate the pages controller in it's default location (/cake/libs/controller/pages_controller.php), but then we would probably forget the next time we update Cake to its latest version. Instead, let's copy it into our /app/controllers directory. Now we can change it all we want, and it will always be with us.
Sure, it's nice that our about page maps to /pages/about and our contact page to /pages/contact. But what if we have to get rid of the /pages/ business? Assuming that pages is our default, catch-all, controller, this should work:
Now we're "cooking with gas" and we have our beloved /about and /contact urls without having to map every single page in the router. What's next? What if we need some custom controller action before our contact page? All we have to do is add one line to the display() function.
Works like magic! Now, if you have a contact() function in your controller, it will first process any logic you have before calling the view.
Got any more snazzy ways to jazz up the Pages Controller?
Initial Installation
Of course, we could manipulate the pages controller in it's default location (/cake/libs/controller/pages_controller.php), but then we would probably forget the next time we update Cake to its latest version. Instead, let's copy it into our /app/controllers directory. Now we can change it all we want, and it will always be with us.
Ultra Clean URLs
Sure, it's nice that our about page maps to /pages/about and our contact page to /pages/contact. But what if we have to get rid of the /pages/ business? Assuming that pages is our default, catch-all, controller, this should work:
$Route->connect('/*', array('controller' => 'pages', 'action' => 'display'));
Give Control to the Controller
Now we're "cooking with gas" and we have our beloved /about and /contact urls without having to map every single page in the router. What's next? What if we need some custom controller action before our contact page? All we have to do is add one line to the display() function.
Controller Class:
<?php
function display() {
...
// add this snippet before the last line
if (method_exists($this, $page)) {
$this->$page();
}
// here's the last line
$this->render(join('/', $path));
}
?>
Works like magic! Now, if you have a contact() function in your controller, it will first process any logic you have before calling the view.
Got any more snazzy ways to jazz up the Pages Controller?








$Route->connect ('(?!admin|items|images)(.*)', array('controller'=>'pages', 'action'=>'display'));
Else the wildcard will knock out your admin routing..
$title = Inflector::humanize(Inflector::underscore($title));
I'd like my content pages (about/faq/contact) to use the pages controller,
but for functional pages, I have a controller with an index method
example.com/users should load the index() in users_controller instead of having to create a users.thtml in the pages dir
Comments are closed for articles over a year old