Wednesday, November 10, 2010

Home Page

While you can route your home page to a controller and action as we did previously, you may prefer to have a separate page set up as the leading page into your site. A good practice is to create a homepage controller and view to use as the main page to your site, this way all of the other controllers and views are tied specifically and only to the model to which they belong.

First create /app/controllers/homepages_controller.php
<?php
class HomepagesController extends AppController {

}
?>
Since the home page controller does not have an associated model, if we wanted to access any data we would define the models we want to access with the $uses variable. If the models are associated, such as our "User" model and "Carrier" model, it may not be necessary to include both, but if they are not related, such as the "Drug" model, but you want to include data from that table, you need to include both.
var $uses = array('User', 'Drug');
In our case we won't be using any data for the home page at this time so we need to create an empty array. We'll also need to create an index method.
var $uses = array();

function index() {
    $this->set('title_for_layout', 'Drug-Ed.com : Drug Education and Medication Adherence');
}
Next we need to create the home page view so create /app/views/homepages/index.ctp For now we'll leave this blank.

And the last thing we need to do is change our routing to go to the home page instead of the previously set up users page.

Edit /app/config/routes.php and replace
    Router::connect('/', array('controller'=>'users', 'action'=>'index'));
with
    Router::connect('/', array('controller'=>'homepages', 'action'=>'index'));
Now browse to http://drug-ed.com/ and verify it goes to the homepages index.

No comments:

Post a Comment