CakePHP – Static pages under the root

I’ve recently started using CakePHP, I don’t really understand it too well, but the first thing I noticed was that any static pages you have normally would go under www.site.com/pages. I thought ‘this sucks’, so I’ve made a very simple work-around, which isn’t great, but it seems to work.

The idea is that someone will go to www.site.com/something and then cake will look for the something controller, if it can’t find it, it will normally throw up an error. By altering this error page, we can get cake to do the pages controller action instead of the error one.

So what I did was add the file: views/errors/missing_controller.thtml which contains:

$this->requestAction('pages'.stripslashes($_SERVER['REQUEST_URI']));

That seemed to do it. Not elegant at all, I know, so if you find a nicer way of doing it, or write some nicer code, please give it to me and I’ll post it on here.

9 thoughts on “CakePHP – Static pages under the root

  1. Surely there must be a method using the native CakePHP routing. Have you asked in the Google Groups?

    I have currently employed your method successfully so thank you very much for that. Let me know if you find another way.

  2. Hi,
    with your method I cannot use $this->viewVars to pass data from the static page to the layout.

    Why would I do that? Well I’d like to take advantage of the general layout, as a template, but using particular content for every “static” page, with a particular ccs.

    For example, the page about, is static but I’d like to use the general layout plus a particular css espefic to that page…

    So far I haven’t found a way to do it other than creating a single rout for every static page.

    Thank you, anyway 😉

  3. My alternative of choice was to turn around Siruuk’s solution: I preferred to create a route for each controller, and finally catch the “/*” urls routing them to the pages controller. From an effort point of view I consider the route as just one more step in the setup of each model/view/controller set, whereas I don’t want to have to create a route for each static page.

  4. I’ve been looking for an alternative to the /pages/ thing (which certainly does suck). Like Daniel, I’m also leaning towards the route solution. Tat seems much more elegant than picking up from an error condition.

  5. This is how I am going to do it:

    In the routes file I commented out these two separate lines:
    //Router::connect(‘/’, array(‘controller’ => ‘pages’, ‘action’ => ‘display’, ‘home’));
    //Router::connect(‘/pages/*’, array(‘controller’ => ‘pages’, ‘action’ => ‘display’));

    And I now have this line instead:
    Router::connect(‘/’, array(‘controller’ => ‘pages’));

    So now if I want an /about page I add this route:
    Router::connect(‘/about’, array(‘controller’ => ‘pages/about’));

    Now /about routes to PagesController->about() and uses the pages/about.ctp view

    Using this method means you have to create a new route for each static page.

    Also now the home page will route to PagesController->index()

  6. hi can i use the static pages in side in controller bu i need to run this controller before giving the database details its possible to do ???

    mypage/photos

    this controller will run with out giving the db info inthe database.php file can i run this controller ???

    i write the router also if i give the db inofo correctly mans its running else its trough error “Requires a Database Connection” how can i solve

    help me

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.