Changing the default layout in CakePHP
CakePHP February 11th, 2007If you want to change the default welcome layout of the CakePHP framework you have to overidde it, you can do this by creating a default.thtml file in the app/views/layouts directory. Once you have done that you have only to specify to Cake to insert the controller view code in to the right place (our default layout in this case). You can to this by including the $content_for_layout and optionally $title_for_layout variables.
You can customize as you want your template by uploading images and styles in the webroot directory app/webroot which becomes your site root from now on.
Example:
After this is done you will see that cake changed the default layout with the one of yours and also the content of the page is cake successfully installation text. Now we have to insert in the new layout our own content. A quick way to do this is creating a controller in app/controllers named as we want but according to cake rules it has to be a plural, for example sites_controller.php . Inside of the controller we will have following code:
-
class SitesController extends AppController {
-
function index(){
-
// here will be your code;
-
}
-
}
var $uses = array(); I use this because I don’t want Cake engine to look for a model regarding this controller. If you omit this line you will get a “missing model” error.
Browsing the http://example.com/sites/index it should reveal a page having your layout applied but with an error of ”missing view”. To get rid of that error you have to create a file called index.thtml in the app/views/sites/ (you have to create the directory also) file that will be the view for your index action of your Sites controller.
If you want visitors to point directly to a certain action of a controller when they enter your website you can do that using “Route Configuration”. Routing is like a mod_rewrite with the help of which you can map URLs to controllers actions and params and back. Routes are configured in app/config/routes.php .
You can find more information about this at the Chapter Configuration on Cake’s Manual.
In our case our routing rule should look like this:
‘/’ – represents the URL we want to map, the domain itself in this case;
‘sites’ – is the controller we wish to invoke
‘index’ – the action we wish to invoke
Now when we point on http://example.com/ Cake engine should display the following page http://example.com/sites/index (sites controller with the index action).
Recent Comments