Chapitre 7. Zend_Controller

Table des matières

7.1. Zend_Controller Quick Start
7.1.1. Introduction
7.1.2. Quick Start
7.2. Zend_Controller Basics
7.3. The Front Controller
7.3.1. Overview
7.3.2. Primary Methods
7.3.3. Environmental Accessor Methods
7.3.4. Front Controller Parameters
7.3.5. Subclassing the Front Controller
7.4. The Request Object
7.4.1. Introduction
7.4.2. HTTP Requests
7.4.3. Subclassing the Request Object
7.5. The Standard Router: Zend_Controller_Router_Rewrite
7.5.1. Introduction
7.5.2. Using a router
7.5.3. Basic Rewrite Router operation
7.5.4. Default routes
7.5.5. Base URL and subdirectories
7.5.6. Route Types
7.5.7. Using Zend_Config with the RewriteRouter
7.5.8. Subclassing the Router
7.6. The Dispatcher
7.6.1. Overview
7.6.2. Subclassing the Dispatcher
7.7. Action Controllers
7.7.1. Introduction
7.7.2. Object initialization
7.7.3. Pre- and Post-Dispatch Hooks
7.7.4. Accessors
7.7.5. View Integration
7.7.6. Utility Methods
7.7.7. Subclassing the Action Controller
7.8. Action Helpers
7.8.1. Introduction
7.8.2. Helper Initialization
7.8.3. The Helper Broker
7.8.4. Built-in Action Helpers
7.8.5. Writing Your Own Helpers
7.9. The Response Object
7.9.1. Usage
7.9.2. Manipulating Headers
7.9.3. Named Segments
7.9.4. Subclassing the Response Object
7.10. Plugins
7.10.1. Introduction
7.10.2. Writing Plugins
7.10.3. Using Plugins
7.11. Using a Conventional Modular Directory Structure
7.11.1. Introduction
7.11.2. Specifying Module Controller Directories
7.11.3. Routing to modules
7.11.4. Module or Global Default Controller
7.12. MVC Exceptions
7.12.1. Introduction
7.12.2. How can you handle exceptions?
7.12.3. MVC Exceptions You May Encounter
7.13. Migrating from Previous Versions
7.13.1. Migrating from 0.9.2 to 0.9.3 or newer
7.13.2. Migrating from 0.6.0 to 0.8.0 or newer
7.13.3. Migrating from 0.2.0 or before to 0.6.0

7.1. Zend_Controller Quick Start

7.1.1. Introduction

Zend_Controller is the heart of Zend Framework's MVC system. MVC stands for Model-View-Controller and is a design pattern targeted at separating application logic from display logic. Zend_Controller_Front implements a Front Controller pattern, in which all requests are intercepted by the front controller and dispatched to individual Action Controllers based on the URL requested.

The Zend_Controller system was built with extensibility in mind, either by subclassing the existing classes, writing new classes that implement the various interfaces and abstract classes that form the foundation of the controller family of classes, or writing plugins or action helpers to augment or manipulate the functionality of the system.

7.1.2. Quick Start

If you need more in-depth information, see the following sections. If you just want to get up and running quickly, read on.

7.1.2.1. Create your filesystem layout

The first step is to create your file system layout. The typical layout is as follows:

application/
    controllers/
        IndexController.php
    models/
    views/
        scripts/
            index/
                index.phtml
        helpers/
        filters/
html/
    .htaccess
    index.php

7.1.2.2. Set your document root

In your web server, point your document root to the html directory of the above file system layout.

7.1.2.3. Create your rewrite rules

Edit the html/.htaccess file above to read as follows:

RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php

The above rules will route any non-resource (images, stylesheets) requests to the front controller. If there are other extensions you wish to exclude from the front controller (PDFs, text files, etc), add their extensions to the switch, or create your own rewrite rules.

[Note] Note

The above rewrite rules are for Apache; for examples of rewrite rules for other web servers, see the router documentation.

7.1.2.4. Create your bootstrap file

The bootstrap file is the page all requests are routed through -- html/index.php in this case. Open up html/index.php in the editor of your choice and add the following:

<?php
require_once 'Zend/Controller/Front.php';
Zend_Controller_Front::run('/path/to/app/controllers');

This will instantiate and dispatch the front controller, which will route requests to action controllers.

7.1.2.5. Create your default action controller

Before discussing action controllers, you should first understand how requests are routed in Zend Framework. By default, the first segment of a URL path maps to a controller, and the second to an action. For example, given the URL http://framework.zend.com/roadmap/components, the path is /roadmap/components, which will map to the controller roadmap and the action components. If no action is provided, the action index is assumed, and if no controller is provided, the controller index is assumed (following the Apache convention that maps a DirectoryIndex automatically).

Zend_Controller's dispatcher then takes the controller value and maps it to a class. By default, it Title-cases the controller name and appends the word Controller. Thus, in our example above, the controller roadmap is mapped to the class RoadmapController.

Similarly, the action value is mapped to a method of the controller class. By default, the value is lower-cased, and the word Action is appended. Thus, in our example above, the action components becomes componentsAction, and the final method called is RoadmapController::componentsAction().

So, moving on, let's now create a default action controller and action method. As noted earlier, the default controller and action called are both index. Open the file application/controllers/IndexController.php, and enter the following:

<?php
/** Zend_Controller_Action */
require_once 'Zend/Controller/Action.php';

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $this->render();
    }
}

The call to render() will render a template. By default, Zend_View is used as the View layer in the MVC. render() does some magic, and uses the controller name (e.g., index) and the current action name (e.g., index) to determine what template to pull. By default, templates end in the .phtml extension, so this means that, in the above example, the template index/index.phtml will be rendered. Additionally, Zend_Controller_Action automatically assumes that the directory views at the same level as the controller directory will be the base view directory, and that the actual view scripts will be in the views/scripts/ subdirectory. Thus, the template rendered will be found in application/views/scripts/index/index.phtml.

7.1.2.6. Create your view script

As mentioned in the previous section, view scripts are found in application/views/scripts/; the view script for the default controller and action is in application/views/scripts/index/index.phtml. Create this file, and type in some HTML:

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>My first Zend Framework App</title>
</head>
<body>
    <h1>Hello, World!</h1>
</body>
</html>

7.1.2.7. View the site!

With your first controller and view under your belt, you can now fire up your browser and browse to the site. Assuming example.com is your domain, any of the following URLs will get to the page we've just created:

  • http://example.com/

  • http://example.com/index

  • http://example.com/index/index

You're now ready to start creating more controllers and action methods. Congratulations!