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

Awesome! I have been looking for a way to do exactly this without creating a PagesController, or any database nonsense.
Aaron Thies said this on January 23, 2008 at 6:23 pm
Thanks !
protospike said this on July 9, 2008 at 6:15 pm
[...] Routing for Static Pages Share and Enjoy: [...]
Aaron Thies » Blog Archive » Remove Pages from Static Pages said this on July 28, 2008 at 3:21 am
Excellent tip!
Luke said this on August 7, 2008 at 6:49 am
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.
Ben said this on August 24, 2008 at 7:50 am
I’m using cakePHP 1.2 RC2 and it has a default routing for static pages into a URL (http://foo.com/pages/contact)
Hazem said this on September 14, 2008 at 9:32 am
But I found this very useful anyway
Hazem said this on September 14, 2008 at 9:33 am
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.
Richard@Home said this on September 14, 2008 at 12:35 pm
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.
Mathieu said this on September 15, 2008 at 1:00 pm
@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?
Richard@Home said this on September 15, 2008 at 9:23 pm
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
Ravi said this on April 19, 2009 at 11:39 am
thanks buddy keep up the good work
kannan said this on July 30, 2009 at 11:25 am
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
leo said this on September 7, 2009 at 7:29 am