Yesterday we left off with a basic folder structure in a Subversion repository, including the Zend Framework libs. It isn’t really much to look at, and it just doesn’t do anything. Not impressive. Let’s fix that.

Today we’ll be covering the front controller and some basic modules and actions. You still won’t need a database at this point, but you will need to have Apache configured correctly. You’ll also want to have the Zend Framework Reference Guide within easy reach.

Before we get started, make sure that your Apache configuration (we’re assuming you’re doing this on a stanadlone server, your local machine using MAMP or WAMP, or a virtual host, and not on shared hosting) is set up so that your document root is your /html folder, and AllowOverrides is set to All for the directory. If you don’t know how to do this, refer to your Web Server’s documentation or ask someone.

The first thing we’re going to do is set up your front controller. Every time someone accesses your application, they access the front controller, and the front controller is responsible for loading up everything else that the user needs to see or the application needs to process. The front controller is one of the few PHP files that lives in your /html directory. Let’s create it, and call it index.php.

The first few things that your front controller needs to know are where the front controller itself is, what the path to the Zend libraries are, and where your controllers are so that it can dispatch the request to them.

Let’s tackle the location first. I generally don’t depend on the file to know where its by using the

dirname(__FILE__);

trick — I tend more towards setting the specific directory structure based on a location variable set in my apache configuration using SetEnv. So the first few lines of my front controller looks like:

if($_SERVER['ENV'] == 'dev') {
    define('BASE','/Users/kkatzke/work/firstapp');
alit} else {
    define('BASE','/var/www/sites/firstapp.katzke.net');
}

From there, you use the base directory constant to add the appropriate paths to your PHP include path. In your case, you’re working in your home directory, so your path would look like ‘/home/myusername/myprojectname/lib/Zend’. BASE would equal ‘/home/myusername/myprojectname/’, so you would add to your index.php:

set_include_path(BASE.'lib/');

Now it’s time to use your first big of Zend Framework magic. Zend Framework uses the Standard PHP Library (SPL) Autoload functionality. All of the Framework libraries are named the same as their path from the base — so the object Zend_Form_Element_Text is in Zend/Form/Element/Text.php. Let’s go ahead and enable Zend Loader, and then I’ll explain what it does.

require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();

Now that Zend Loader has registered an autoload handler, any time you want to use an object within the Zend Framework, you don’t have to be sure you’ve called ‘require_once()’ yet — it’ll “autoload” itself in based on the name. Let’s go ahead and use that to set up the MVC Layout… but we’ll leave it commented out for now. You’ll find out why later.

#Zend_Layout::startMvc();

Note, again, that we didn’t have to include any extra files to access the Zend_Layout option! Pretty cool, huh? Let’s hurry up and get a site working. The last part of the Front Controller is the Dispatch processor. Dispatching is like saying ‘ok, our environment is set up, let’s go’ within Zend — it takes the request, does some magic, and spits out a response. It should be the last thing that happens in your front controller.

Zend_Controller_Front::run(BASE.'app/controllers');

There’s a lot more that could be said about the front controller and dispatch loop/process, but you can go peruse it in the manual — we’re focused mainly on getting a new app up as fast as we can.

Now, note when we called that front controller, we addressed the BASE.’app/controllers’ directory — this is where we’re going to work next. First, make sure your Front Controller (index.php) file looks something like this, and then let’s move on.

if($_SERVER['ENV'] == 'dev') {
    define('BASE','/users/home/myusername/myprojectname/');
alit} else {
    define('BASE','/home/myusername/myprojectname/);
}
 
set_include_path(BASE.'lib/');
 
require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();
 
#Zend_Layout::startMvc();
 
Zend_Controller_Front::run(BASE.'app/controllers');

Modules

You need two basic modules and a few templates to have your first basic, working application. Let’s add an index module and a template.

In BASE./app/controllers, let’s create a file named IndexController.php. Capitals are important here. It should look something like this:

class IndexController extends Zend_Controller_Action {
 
  public function indexAction() {
 
  }
 
}

Again, we’re using the magic of the autoloader. While we’re in the directory, let’s also create an error controller that will tell us what we did wrong. The file will be called ErrorController.php

class ErrorController extends Zend_Controller_Action {
 
  public function errorAction() {
    $errors = $this->_getParam('error_handler');
    $exception = $errors->exception;
 
    echo $exception->getMessage();
    echo "<br /><pre>";
    echo $exception->getTraceAsString();
  }
}

Now, we need to create some view scripts. You’ll need to create some folders. From BASE, you’ll want to create /app/views/scripts/index and /app/views/scripts/error — and then create index.phtml and error.phtml in the corresponding directories. Note: The name of the directory in scripts matches the CONTROLLER name, and the name of the .phtml file matches the ACTION name. So if we were to create an action in index called ‘about’, you would then create an ‘about.phtml’ file in the /app/views/scripts/index directory.

Open up the index.phtml you just created and type ‘hello world’. Then open up the error.phtml file that you created and type ‘Oops, error!’.

Last but not least, we need an .htaccess file in BASE/html/.htaccess to tell Apache where to route things. Here’s the one I use:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

Now, the moment of truth — point your browser at your document root, and you should see “hello world”! If you got a blank screen, check your PHP error log. If you got a zend framework error printed to your screen, you know your error handler works..! Just figure out what went wrong and fix it.

Tomorrow we’re going to work with the database by adding some database boostrap logic to the front controller, and we’ll retrieve and display some rows with Zend_Db_Table.

2 comments on ' Your First Zend Framework Application: Day Two '

  1. Hi Karl,

    I additionally needed in my .htaccess this line:
    RewriteBase /

    Regards,
    Horst

  2. Ups, the text behind the slash vanished…
    it should be /alias of the application

Leave a comment

name (req'd)

email (req'd)

website