Saturday, January 27, 2007

Setting Up the Zend Framework

Setting up the Zend Framework is easy even on shared hosts since no binaries need to be installed. But if you don't know the intricacies of Apache and PHP, certain requests can fail to work for seemingly no apparent reason.

I originally learned from an older version of a tutorial called Getting Started with the Zend Framework [pdf] from Akra's DevNotes, so I will follow his lead with a few stylistic choices. You may want to check out that tutorial or a newer version of it if it's out.

This is just a bare-bones setup. Nothing fancy. No databases, forms, etc. Just "hello world" to get you started. You'll want to get this working before moving on, of course.
  1. Zend Library
    I'm using version 0.7.0. Unzip ZF and copy the library directory (ZendFramework-0.7.0/library/) into your webroot so that you have /webroot/library/. This directory should contain Zend.php and a Zend subdirectory. This is really all you need to start using Zend, but there are a few standard things that still need to be done for a website.
  2. PHP Config
    You'll want to make sure the following two php.ini settings are set.
    register_globals = Off
    magic_quotes_gpc = Off


    If you're using PHP as an Apache module, remember to restart Apache for changes to take effect.
  3. Apache Config
    Right now, anyone can navigate to or request a file in your library directory. To prevent this, use a .htaccess file in the library directory, /webroot/library/, with the following directive. You will also want to copy this into your application directory, /webroot/application/, where you store all your application-specific MVC files.
    deny from all
  4. Bootstrapping
    Bootstrapping allows all URL requests to be processed through a single entry-point, called the bootstrap file. This way, URLs can be mapped to controller actions. In /webroot/.htaccess, add the following lines which internally redirect all URLs to the index.php file.
    RewriteEngine on
    RewriteBase /
    RewriteRule .* index.php


    However, certain files like client scripts, style sheets, and images should be served as normal without involving the framework. So in your public directory, /webroot/public/, where such files are stored, add the following in another .htaccess file.
    RewriteEngine off

    If you're using phpMyAdmin in a subdirectory on a local server, you'll want to do the same for that directory to serve files as usual.

    Now /webroot/index.php is the bootstrap file and the entry-point for all requests, besides in the /webroot/public/ directory.
  5. Standard Bootstrap File and Dispatching
    The following is a bare-bones bootstrap file that uses standard routing. Put this in /webroot/index.php.
    <?php
    error_reporting(E_ALL|E_STRICT);
    date_default_timezone_set('America/New_York');

    // Load Zend.
    set_include_path('.' . PATH_SEPARATOR . './library'
    . PATH_SEPARATOR . './application/models/'
    . PATH_SEPARATOR . get_include_path());
    include 'Zend.php';

    // Load classes used on all requests.
    Zend::loadClass('Zend_Controller_Front');

    // Setup controller.
    $frontController = Zend_Controller_Front::getInstance();
    $frontController->setControllerDirectory('./application/controllers');

    // Turn on display of exceptions.
    $frontController->throwExceptions(true);

    // Dispatch and run.
    $frontController->dispatch();
    Leaving out the closing ?> is intentional since including it in pure PHP files can lead to unwanted output and hard-to-detect bugs.
  6. Index Controller and Standard Routing
    To set up a simple index page, create a new file /webroot/application/controllers/IndexController.php and add the following.
    <?php

    class IndexController extends Zend_Controller_Action
    {
    function indexAction()
    {
    echo '<p>in IndexController::indexAction()</p>';
    }

    function fooAction()
    {
    echo '<p>in IndexController::fooAction()</p>';
    }
    }

    Now, visiting http://hostname/ should display a page with "in IndexController::indexAction()", and visiting http://hostname/index/foo/ should display a page with "in IndexController::fooAction()". Of course, due to the way the default router works, all the following URLs display the same thing.
    http://hostname/
    http://hostname/index
    http://hostname/index/
    http://hostname/index/index
    http://hostname/index/index/
    The default router looks at a URI and matches against the pattern /controller/action/param1/value1/param2/value2. http://hostname/ defaults to the index action of the index controller, which will be identical to http://hostname/index/index. This is also identical to http://hostname/index/index/ because the default router trims leading and trailing slashes. (Curiously, http://hostname/index returns a 400 Bad Request error on my shared hosting site; I'm not sure why. I never noticed this before.)

    If you're having the problem of navigating to http://hostname/index/index failing (returning a 404) even when http://hostname/ works — a problem I ran in to working on a shared host — you must make a dummy /webroot/index/ directory with a .htaccess file telling Apache to use the rewrite router. In other words, create the directory /webroot/index/, and copy /webroot/.htaccess into it. This must be done for each subdirectory (equivalently, each controller) that you wish to have. So if you want /downloads/ to be a valid URI, you must make a /webroot/downloads/ directory. If you want /archives/ to be a valid URI, you must make a /webroot/archives/ directory, and so on. Each with the .htaccess file specifying the RewriteRule. The cause for this has to do with Apache's configuration. However, I haven't been able to determine the exact reason since the shared host I'm using does not allow access to the config file.

1 comment:

Gaurav Mudgil said...

hi i gone through your tutorial and i faced the same problem as you faced , when i call http://localhost/gauravcvs/zendexample/

it runs and call's the dufault controller and action as index

but when i run http://localhost/gauravcvs/zendexample/index

it gives page not found 404 error

but i tried your solution as you said create an directory /zendexample/index/
and copy the /zendexample/.htaccess to this /zendexample/index folder

but it still does not wrk for me any help would be appriciated .

with regards
Gaurav Mudgil