CakePHP Tip: Routing for static pages

Woo, my first CakePHP Tip!

Add the following to /app/config/routes.php:

Router::connect(‘/contact’, array(‘controller’ => ‘pages’, ‘action’ => ‘display’, ‘contact’));

and create your contact page at:


You can now access the contact page at:


Software developer by day, scale model builder and wargamer by night.

Posted in CakePHP
23 comments on “CakePHP Tip: Routing for static pages
  1. Aaron Thies says:

    Awesome! I have been looking for a way to do exactly this without creating a PagesController, or any database nonsense.

  2. protospike says:

    Thanks ! 😉

  3. […] Routing for Static Pages Share and Enjoy: […]

  4. Luke says:

    Excellent tip!

  5. Ben says:

    And what’s great about this, is if you’re using the Auth component in app_controller.php, it still does all the auth stuff if you have it in a beforeFilter in app_controller.php. Again, for anybody that’s reading, if you have an app that needs authentication, but needs authorization on some, but not all pages, you can use this tip to make your non-authenticated pages.

  6. Hazem says:

    I’m using cakePHP 1.2 RC2 and it has a default routing for static pages into a URL (

  7. Hazem says:

    But I found this very useful anyway

  8. Richard@Home says:

    Hazem, the tip means you don’t need the /pages/ part of the url. It means you can have url’s like:


  9. Mathieu says:

    I just changed my route to the following:

    Router::connect(‘*’, array(‘controller’ => ‘pages’, ‘action’ => ‘display’));

    This way you don’t need to create a new one for each static page, and works for any in the pages folder.

  10. Richard@Home says:

    @Mathieu: I *think* that’s great for a site with all static pages, but I think you might hit a problem with dynamic pages that uses a controller other than the pages controller.

    Basically, you’re routing rule says: For every page, pass it to the page controllers display method.

    What will you do if you need to display the Users->login() action?

  11. Ravi says:

    Richard, It is a good tip. But arent you hard coding the static content into a view. I know it works best for a website with minimal static content, but in implementing a CMS with lots of static content, it may not be useful to hard code the static content into a view. What say?

    But it is a clever idea nevertheless.


  12. kannan says:

    thanks buddy keep up the good work

  13. leo says:

    iam new to cakephp
    i have followed all these steps when i test in on a local server
    it shows my only a blank page, (no contents are shown in the page )

    Please help me regarding static page

  14. AR says:

    I am having the same issue. I followed the directions, but no contents are shown when I try to access the page. Any ideas why it’s not showing up?

  15. Kate C2 says:

    Extremely useful, cheers for sharing this excellent Cake PHP tip on static pages without the /pages/ bit.

  16. JP says:

    If you don’t want to have the default layout associate with the static view.ctp then just place with at the very top of your view.ctp file layout = null; ?>

  17. Philip says:

    For those of you who are having difficulty displaying the page (a blank page or a 404 error), make sure that you have the proper .htaccess file(s) in your folders. Refer to this page for directions….

    I was getting 404s and it’s because I didn’t have my .htaccess files.


    Hope this helps!

  18. Alex says:

    . Hi, i really liked this article.. Thanx for sharing. . it prove very beneficial for me.

  19. Mercy says:

    I have created the static page by using your post,but that is not working. advice me.

  20. bhawna says:

    Hi, I tried your tip but it keeps taking me to default page.

  21. Ankur Jain says:

    Very Helpful post for me.
    keep posting…

  22. Genesis says:

    Great! This is what I need…

  23. Oecqpful says:

    I’d like to send this letter by teen nymphet erotica The chick is hot and the dude is crazy ;D But I feel like the girl is doin to much with the extra

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: