Your CakePHP application's Homepage
Posted on 12/12/08 by Felix Geisendörfer
Rob Wilkerson and I just talked about what the best place is to put your homepage in CakePHP. My answer was the following:
If your homepage is static, just use your PagesController to render a home.ctp view.
If your homepage is dynamic, add a homepage method to your PagesController:
public function view() {
// Your normal pages
}
public function homepage() {
$posts = ClassRegistry::init('Post')->find('all');
$this->set(compact('posts'));
}
}
Don't forget to update your routes. Some apps may also simply point their home route to the index function of another controller. How do you handle homepages in your app?
-- Felix Geisendörfer aka the_undefined
You can skip to the end and add a comment.
Rob Wilkerson: You could rename the view, there is really nothing magic to it. Doesn't really matter in the long run either : ).
In most cases I create a dedicated controller: IndexController
The controller contains severeal methods for homepages, i.e. one for unregistered users, one for registered and so on.
to this point, i have only used routing to a function of one of my controllers. eg. for an online training signup app i routed the home page to a controller listing current courses.
Of course it depends on the specifics, but I tend to agree with dave that making a new "/" route to some controller and action is the easiest way to get a dynamic homepage.
p.s. as I've shamelessly exemplified here :)
http://teknoid.wordpress.com/2008/11/17/get-yourself-a-new-home-alternative-to-homectp/
I usually use the same method you described for websites. However I created a dashboards controller, for the last application I've build, to act as the homepage of the application
I usually changing only the routes. Most of the time I am writing a system where some of the index action is enough to be home page of it. For websites - I am using CMS :)
I generally build standard content management systems so I need all pages to be editable for my clients.
I setup the '/' route to point to the view method of the pages controller and pass an argument of 'index'. The view method is setup to expect a slug argument which it then uses to lookup in the database.
$Route->connect('/', array('controller' => 'pages', 'action' => 'view', 'index.html'));
//pages_controller.php
function view($slug = null) {
if(!$slug || $this->Page->findCount(array('Page.slug' => $slug)) == 0) {
$this->cakeError('error404', array(array('url' => strtolower('/pages/'.$slug), 'message' => 'Was not found on this server', 'base' => $this->base)));
}
$this->set('page', $this->Page->findBySlug($slug));
}
So, do you mean you placed a modified copy of pages_controller.php from CakePHP in /app/controllers? Trying to do that, I just get a "Missing Database Table" error.. Or am I missing the point here?
Oscar: Put in var $uses = array(); into the copied PagesController.
This is to overwrite cake's convention to seek a Page model (which is non existent in your app).
Ah, of course, thanks, got it working now :)
You'r welcome. ; )
I generally use IndexController for my home page, I think. I'm not that great with PHP or Cake though.
Hi. I also needed data to be pulled on the homepage which is just basically to display summaries of some tables for the quick view of a user.
I have this now
< ?php
class PagesController extends AppController {
var $uses = array();
function home() {
echo 'a';
}
}
?>>
But when I try to access the normal "http://[site]/", it asks me for the "display" method and view. And when I add the file and method - it then uses that instead of the home.ctp. why is that? I already have the home function and view. why not that?
I hope i kinda explained it clearly.
This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.
Just thought I'd jump in with a little context...
I was using all of the defaults - routes, home.ctp, etc. - and I realized that I wanted my homepage to pull a little data. This is data I needed for the homepage only, so an element seemed unnecessary. I also didn't want to modify PagesController->display() and I didn't want to have the entire PagesController load the model I needed via $uses because I only needed that model on the homepage.
Felix's solution worked perfectly except for one thing. Because I was using the default home.ctp file, I had to name the new method PagesController->home() (though maybe I could've specified the name of the view template to use in the method itself?).
Thanks again, Felix.