Mar 4 2009

Starting with Zend Framework – Setup your development environment

This article intends to make you in Zend Framework quickly and painless. I will try to show how can you setup your project in less than 30 minutes.

First Task: Download the library

Download Zend Framework

Second Task: Define your directory structure

This depends mostly from the way you work, on my personal case, i preffer the following directory structure:

  • /
  • /public
    • /config
    • /controllers
    • /models
    • /views
      • /layouts
      • /scripts
  • /shared
    • /Zend

Third Task: Set your project virtual hosts

[sourcecode lang=’php’]<VirtualHost localhost:80>
ServerName localhost
ServerAlias localhost
ServerAdmin webmaster@localhost
DocumentRoot /<DOCUMENT_ROOT>/public
CustomLog /var/log/apache2/localhost-access.log combined
</VirtualHost>[/sourcecode]

Please notice on <DOCUMENT_ROOT>, this should be the folder structure to you newly created directory structure for the project.

Fourth Task: Create the htaccess file to redirect all requests to your bootstrap file

Go to your <DOCUMENT_ROOT>/public/ folder and create the .htaccess file with the following content.

[sourcecode lang=’php’]RewriteEngine on
RewriteBase /
RewriteRule !\.(js|css|ico|gif|jpg|png)$ bootstrap.php[/sourcecode]

 

This way you are defining that all your requests, excluding all the js,css,ico,jpg and jpg file calls will go to bootstrap.php file.

Fifth Task: Create your bootstrap file

Your bootstrap content will look like.

 

[sourcecode lang=’php’]<?php
    /**
     * Bootstrap file
     */
set_include_path(‘.’ . PATH_SEPARATOR . ‘../shared/’);
//include path to zend framework and models

date_default_timezone_set(‘Europe/Lisbon’);
//timezone and time options

/**
 * Zend Front Controller
 */
require_once ‘Zend/Controller/Front.php';

/**
 * Enabling auto loading
 */
Zend_Loader::registerAutoload();

$controller = Zend_Controller_Front::getInstance();

/**
 * Set the path to controllers
 */
$controller->setControllerDirectory(‘./controllers/’)
            ->setBaseUrl(‘/’)
            ->throwExceptions(true);

/**
 * Starting the MVC Layer
 */
Zend_Layout::startMvc(array(“layoutPath”=>”views/layouts”));

$response = $controller->dispatch();   

?>[/sourcecode]

Now that you have your bootstrap file, meaning your request router, you are ready to make your default controller.

Sixth Task: Create the layout file

As you may imagine, you need to create a layout for your project, in this quick how-to we will use the Zend Layout component. On your /public/views/layout/ folder create the file layout.phtml with the following content.

[sourcecode lang=’html’]<?php echo ‘<?xml version=”1.0″ encoding=”UTF-8″?>'; ?>

<?php echo $this->doctype(‘XHTML1_TRANSITIONAL’); ?>

<html xmlns=”http://www,w3,org/1999xhtml”>

    <head>
   
        <?php echo $this->headMeta()->appendHttpEquiv(
            ‘Content-Type’,
            ‘text/html; Charset=UTF-8′
        ); ?>
    </head>

    <body>
     <div id=”header-navigation” style=”float: right”>
        <a href=”<?= $this->url(
            array(‘controller’=>’login’),
            ‘default’,
            true) ?>”>Login</a>
    </div>
        <?php echo $this->layout()->content; ?>   
    </body>

</html>[/sourcecode]

Seventh Task: Your first controller

You need to create the controller that will respond to your first request, we call it the Index Controller. To create it just go to your /public/controllers directory and create a file named IndexController.php with the following content.

[sourcecode lang=’php’]<?php

require_once ‘Zend/Controller/Action.php';

class IndexController extends Zend_Controller_Action {
   
    public function indexAction() {
    }
}
?>[/sourcecode]


Eight Task: Your first view

In order to display something on your controller request handler you need to create the view, we call it the Index View.

Go to you views folder placed under /public/views/scripts/ and create a folder named index, the name of your first controller, create the action name file inside it, as we are handling the action “Index” you should create a file named index.phtml.

[sourcecode lang=’html’]Hello world on INDEX[/sourcecode]

 

In this article i have approached the View and Controller, because this is a Starting with Zend Framework i haven’t approached the Model thematic, i will make an approach to it on next article. There are plenty of contents to explore on the Zend Framework, this article intends to put your on the first step, having your work environment set, the fun begins after this.

5 comments on “Starting with Zend Framework – Setup your development environment

  1. Is there a way to by pass the “Third Task: Set your project virtual hosts”. Just thinking if there is a way to put the modification on config file or ini file.

    Thanks in advance!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>