Skip to content

How to create a controller

adamthedeveloper edited this page Sep 14, 2010 · 3 revisions

In app/controllers, create a controller – let’s call it CowController.php and put the following inside:


<?php class CowController extends ApplicationController { function mooAction() { $this->layout = 'default'; $this->sound = 'moooooo'; $this->render('cow/moo'); } }

We want our controllers to extend ApplicationController. ApplicationController is extended by all controllers so that you can add site wide functions and variables if needed. Also, camel casing the name is required.

Here, we created our first action called mooAction(). Inside the moo action, we specifiy which layout we want to use. You can add your layouts in app/views/layouts. The filename for layouts and view scripts must end in phtml – however, you can change that if you wish in the bootstrap.php file.

After specifying the layout, we created an instance variable called sound and finally, we render our view script. Next, we will create our view script.

Clone this wiki locally