Showing posts with label ZF. Show all posts
Showing posts with label ZF. Show all posts

Monday, July 15, 2013

Create new controller on ZF

In this tutorial I'll explain how to create new controller and do more interesting stuffs. As I explained in previous post, for most important of controller, that requires directory with same name of controller and inside that, view pages with same name of all actions of controllers. But, this is automatically managed by ZF command line tool. And in Netbeans IDE there is more good interface for executing commands

- in Netbeans project panel --Right Click on project > Zend > Run Command... and interface will be open.


  • First parameter is "Filter": That similar to search particular command from all command list.
  • Matching Tasks: This will show you commands matching your query entered in Filter textbox. Select proper command from list. There is text area below list of commands, it'll show you description.
  • Enter required parameters in "Parameters" textbox.
  • At very bottom, there is "commad", it'll show you final output command. 
  • and press Run to execute command.
  • For example, I've created controller named "info".










[controller]

[view]


As Shown above both images, you can see new created controller: "InfoController.php" and for that controller there is "info" directory with .phtml files of actions of controller.

Now, let's modify Info Controller:
- Remove content of index.phhtml and edit indexAction in Controller.








In any controller there is init() and indexAction() as by default. You can create as many functions you want. When any controller executes init() method always executes first, no matter which action you are executing from that controller.

Now, Lets create another action in info controller with its view:


and add some message in anotherAction() function...

and execute...


That's it. This is enough to get started with ZF Controllers.

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.