Monday, July 15, 2013

Controller understanding in ZF

When we create new ZF project, it creates two controllers [ IndexController.php & ErrorController.php ] by default. Controller as also called as Action Controller, because it controls all actions. Controllers are located in the application/controllers  directory. The action controller is concerned with out application's control logic and is part of the 3-tier separation the Model-View-Controller offers.

Default Example of controller:

class IndexController extends Zend_Controller_Action                                                                    
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }


}



Most Important:


Once one controller is created, then there is some thing you must care about. For example, for IndexController.php, with controller its view file is also created. As shown in image, there is naming conversation for that also. Views directory stores all view files of application. Inside views/scripts,  there must be directory with the same name of controller.
So, in this example there must be index directory. And based on action method in controller there should be view file. So, one view file for one action. So, here in this example, there is indexAction in IndexContoller.php, so, in views/scripts/index there is index.phtml


How controller executes:

We can directly call controller from addressbar. example: 
http://zf2-local.com executes indexAction() of indexController.php.
so, if you execute http://zf2-local.com/index/ then also output will be same. as like that, 
http://zf2-local.com/index/index will also give same output.






In next tutorial we'll modify controllers.