Zend Framework Action Step by Step Tutorial
Master Zend Framework action controllers. Learn how dispatching works, how to create actions, handle parameters, and build multi-action controllers.
What Are Actions in Zend Framework?
In Zend Framework 1.x, every URL maps to a controller and an action. The URL /user/edit calls the editAction() method on the UserController class. This is the front controller pattern - a single entry point dispatches requests to the right code.
Actions are where your application logic lives. They receive input, call models, and pass data to views. Getting comfortable with actions is the foundation for everything else in Zend Framework.
Pack Overview
This pack starts with the basics - getting Zend Framework running and understanding the directory layout. Once you have a working skeleton, the articles walk through how the dispatcher finds controllers and actions, and how .htaccess rules make clean URLs possible.
Walkthrough
Step 1 - Set Up the Framework
The basic tutorial article gets Zend Framework installed and a “Hello World” page running. You will download the library, place it in your project, and configure the autoloader.
The critical file is index.php in your public directory. It sets up the include path, initializes Zend_Application (or bootstraps manually), and calls dispatch().
Step 2 - Understand the Folder Structure
The folder structure articles explain where controllers, models, views, and configuration files belong. Zend Framework uses a conventional layout:
1 | application/ |
The naming conventions are strict. UserController.php must contain a class named UserController that extends Zend_Controller_Action. The file edit.phtml must live in views/scripts/user/ to match the editAction() method.
Step 3 - Configure .htaccess
The .htaccess article shows how to route all requests through index.php. Without this, URLs like /user/edit return a 404 because there is no physical file at that path.
The rewrite rules are straightforward but they require mod_rewrite to be enabled. If you see a 500 error after adding .htaccess, check that AllowOverride All is set in your Apache virtual host configuration.
Step 4 - Write Your First Action
With the skeleton in place, creating an action is simple. Add a public method to your controller:
1 | public function editAction() |
The view script at views/scripts/user/edit.phtml renders automatically. You do not need to call render explicitly unless you want to use a different template.
How Dispatching Works
The front controller reads the URL and splits it into module, controller, and action segments. It instantiates the controller class and calls the action method. Before and after the action, preDispatch() and postDispatch() hooks run. You can override these to add authentication checks or logging.
The dispatch loop can forward to other actions using $this->_forward('otheraction'). This does not send a redirect - it stays in the same request cycle.
Common Pitfalls
Action name mismatch. The URL segment edit-profile maps to editprofileAction() by default. Hyphens are stripped and the string is lowercased. This catches people who expect camelCase mapping.
Missing view script. If the view file does not exist, Zend throws an exception. Make sure the directory matches the controller name and the file matches the action name.
Not calling parent constructor. If you override __construct() in your controller, you must call parent::__construct() or the request and response objects will not be initialized.
FAQ
What is the difference between an action and a controller?
A controller is a class that groups related actions. An action is a single method in that class. One controller can have many actions.
Can I have actions in subdirectories?
Yes, using modules. Each module has its own controllers/ directory. The URL becomes /modulename/controller/action.
How do I pass data from the action to the view?
Assign it to $this->view->variableName. The view script can access it as $this->variableName.
What if I need to return JSON instead of HTML?
Disable the view renderer using $this->_helper->viewRenderer->setNoRender(true) and output JSON directly with echo json_encode($data). Set the content type header first.
Is Zend Framework 1 still usable?
It works, but it is no longer maintained. The successor project is Laminas. If you are maintaining a legacy project, this pack is directly applicable. For new projects, consider Laminas or another modern framework.
Articles in This Pack
Pack Checklist
- Zend Framework library installed and on the include path
- Apache with mod_rewrite enabled
- A working .htaccess file routing requests to index.php
- PHP 5.2 or later
- Understanding of the MVC pattern