Taking Advantage of the Pages Controller

by joelstein
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!

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?

Report

More on Snippets

Advertising

Comments

  • tgurske posted on 10/21/09 12:41:17 AM
    Thanks!
  • mr_ice posted on 12/19/07 02:33:14 AM
    This maked my day. finaly I have nice looking url's
  • gerhardsletten posted on 04/16/07 03:32:26 PM

    $Route->connect ('(?!admin|items|images)(.*)', array('controller'=>'pages', 'action'=>'display'));

    Else the wildcard will knock out your admin routing..
  • lloydhome posted on 11/05/06 09:14:50 AM
    To change MyCoolPage to My cool page

    $title = Inflector::humanize(Inflector::underscore($title));
  • demark posted on 10/22/06 06:44:14 PM
    Just for reference, you should place the /* route LAST in your routes file or it will override all of the other routes.
  • reversefolds posted on 11/30/99 12:00:00 AM
    how do you keep it from overriding an index() method for a controller?

    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

  • firespade posted on 11/30/99 12:00:00 AM
    I know it sounds kind of lame, but I'm new to cake so this actually helped for what I had in mind. I needed to process my logic in my controller before rendering my page. Thanks a bunch!!!
login to post a comment.