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:

/app/views/pages/contact.ctp

You can now access the contact page at:

http://foo.com/contact

~ by Richard@Home on August 13, 2007.

13 Responses to “CakePHP Tip: Routing for static pages”

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

  2. Thanks ! ;)

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

  4. Excellent tip!

  5. 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. I’m using cakePHP 1.2 RC2 and it has a default routing for static pages into a URL (http://foo.com/pages/contact)

  7. But I found this very useful anyway

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

    http://foo.com/contact
    http://foo.com/about

    etc.

  9. 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. @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. 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.

    -Ravi

  12. thanks buddy keep up the good work

  13. iam new to cakephp
    i have followed all these steps when i test in on a local server
    http://localhost/cake_php/contact
    it shows my only a blank page, (no contents are shown in the page )

    Please help me regarding static page

Leave a Reply