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

About these ads

~ by Richard@Home on August 13, 2007.

23 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

  14. 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. Extremely useful, cheers for sharing this excellent Cake PHP tip on static pages without the /pages/ bit.

  16. 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. 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….

    http://book.cakephp.org/view/37/Apache-and-mod_rewrite-and-htaccess

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

    /.htaccess
    /app/.htaccess

    Hope this helps!

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

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

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

  21. Thanks…
    Very Helpful post for me.
    keep posting…

  22. Great! This is what I need…

  23. I’d like to send this letter by http://doynonoko.de.tl 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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: