nuts and bolts of cakephp

Get yourself a new home (alternative to home.ctp)

Posted in CakePHP by teknoid on November 17, 2008

I’m sure you know that to modify your application’s homepage, one needs to edit/create the home.ctp file in app/views/pages/home.ctp.
That, however, leaves you dealing with a more or less static page…

One common option to add some other functionality to your otherwise static pages is to copy the Pages Controller from the core into your app, and make some required modifications.

On the other hand, you don’t have to be stuck with home.ctp as your default main page. All you need to do is simply designate a new root (/) route.

Let’s look at an example…

I have a Markets Controller (markets_controller.php), which has a nice action called summary(). It pulls data from various Market model methods and utilizes some features of the App Controller.
All of that is working quite nicely and in reality I’d prefer that page (summary) to be my new “home”.

So, rather than dealing with Pages Controller hackery, I simply replace the default route (in app/config/routes.php) with:

Router::connect('/', array('controller' => 'markets', 'action' => 'summary'));

That’s it, now I’ve got a new fully dynamic homepage without any pain whatsoever.

5 Responses

Subscribe to comments with RSS.

  1. sheppy said, on November 23, 2008 at 7:11 am

    Ye that’s what I tend to do, although I create a home controller especially for the homepage as it tends to have some unique content using several different models on it. I usually only have one action defined for it though, being the index.

  2. teknoid said, on November 24, 2008 at 12:42 pm

    @sheppy

    Alright, sounds good ;)

  3. Robin said, on November 28, 2009 at 2:36 pm

    I was searching for this for the last few hours.. thanx…

  4. techblog said, on November 29, 2009 at 8:46 pm

    thats a one good option…however if there are more than one type of models to display in home page we need an alternative method

    good luck

  5. teknoid said, on November 30, 2009 at 10:29 am

    @techblog

    What does it have to do with how many models you need to display?


Leave a Reply