Home arrow PHP arrow Page 2 - Build a Website with the Zend Framework

Create the Front-End Controller - PHP

In this third part of a four-part series on MVC and the Zend framework, you'll start building your first website based on the Zend framework and the model-view-controller pattern. This article is excerpted from chapter 25 of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).

  1. Build a Website with the Zend Framework
  2. Create the Front-End Controller
By: Apress Publishing
Rating: starstarstarstarstar / 2
September 16, 2010

print this article



To begin, create a file named index.php and place the code found in Listing 25-1 inside it. The index.php script is known as the front-end controller and, believe it or not, it will be responsible for ensuring that every request for this application receives the appropriate response. This document should reside in your desired application document root.

Additionally, in the same directory, create a directory named application, and in that directory create a modules directory, and within that a default directory. Finally, within the default directory create two more directories named controllers and views, and within the views directory create a directory named scripts, each of which you'll use later.

Listing 25-1. The Application's Front-End Controller (index.php)


    // Load the Front Controller class

    // Instantiate an instance of the Front Controller Class
$frontController = Zend_Controller_Front::getInstance();

    // Point to the module directory

    // Throw exceptions (useful during debugging)

    // Start the Front Controller


It is assumed the Zend Framework application will reside in the server's document root. However, because this isn't always possible, you can use the setBaseUrl() method to override the front-end controller's default behavior. See the Zend Framework documentation for more information.

The Controllers

Next we'll create two controllers, namely IndexController.php and AboutController.php. These views should be placed in the directory application/modules/default/controllers. First, create the default controller class (IndexController.php), which defines the action that will occur when the Web site's home page is requested (for the sake of consistency throughout the remainder of this chapter we'll refer to http://www.example.com/ as the target domain). This script is shown in Listing 25-2.

Listing 25-2. The IndexController Class (IndexController.php)


    // Load the Zend_Controller_Action class

    class IndexController extends Zend_Controller_Action

        // Accessed through http://www.example.com/
        public function indexAction()
$this->view->title = "Welcome to Our Chess Club Web Site!";



In this example, I've created a view property named title that will be used to assign the Web page's title.

Finally well create one more controller intended to display information pertinent to the Web site's purpose and, for the sake of demonstration, some information about the visiting user. This controller, titled AboutController.php, is displayed in Listing 25-3.

Listing 25-3. The AboutController Controller (AboutController.php)


    // Load the Zend_Controller_Action class

    class AboutController extends Zend_Controller_Action

    // Accessed through http://www.example.com/about/
public function indexAction()
$this->view->title = "About Our Chess Club";

    // Accessed through http://www.example.com/about/you/
public function youAction()
// Page title
$this->view->title = "About You!";

        // Retrieve the user's IP address
$this->view->ip = $_SERVER['REMOTE_ADDR'];

        // Retrieve browser information 
        $this->view->browser = $_SERVER['HTTP_USER_AGENT'];



Please check back next week for the conclusion to this article.

>>> More PHP Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: