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.