Skip to end of metadata
Go to start of metadata

<h1>Zend Framework QuickStart</h1>

<h2>Introduction.</h2>

<p>This document will help Zend Frameworks newcomers to learn and understand how to start their own ZF applications. You can download <a href="http://framework.zend.com/wiki/download/attachments/43103/quickstart.zip">this file</a> where you will find the code used in this quickstart guide.</p>

<p>The following guide will show how to create a very simple blog application using a common page layout and interacting with a database.</p>

<h2>Directories needed</h2>

<p>The following directory structure will be used in this quickstart.</p>

<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
quickstart/
application/
config/
controllers/
layouts/
scripts/
models/
views/
scripts/
library/
public/
]]></ac:plain-text-body></ac:macro>

<p>Create a new directory in your webserver which points to our public folder.</p>

<h2>Creating the database</h2>

<p>Execute the following code in your database:</p>

<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
CREATE DATABASE IF NOT EXISTS quickstart;
USE quickstart;

DROP TABLE IF EXISTS `comments`;
CREATE TABLE `comments` (
`comment_id` int(10) unsigned NOT NULL auto_increment,
`comments` longtext NOT NULL,
PRIMARY KEY (`comment_id`)
);
]]></ac:plain-text-body></ac:macro>

<p>This will create our database and one table that will be used by our quickstart application, this table will store our visitors comments.</p>

<h2>Setting up the rewrite apache rules</h2>

<p>You must create an .htaccess file in the public folder with the following contents:</p>

<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
]]></ac:plain-text-body></ac:macro>

<p>This file will redirect all requests to the index.php file.</p>

<h2>Creating a configuration file</h2>

<p>Create a file with the name quickstart.ini in the /application/config folder with the following contents:</p>

<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
; Quickstart configuration data
[default]
;layout
appearance.layoutPath = ../application/layouts/scripts/
appearance.layout = Main
;database
database.adapter = pdo_mysql
database.params.host = localhost
database.params.dbname = quickstart
database.params.username = quickstart
database.params.password = secret
]]></ac:plain-text-body></ac:macro>

<p>This file contains two configurations needed in our quickstart example.</p>
<ul>
<li>Layout: we will set the path where our layout scrips are and the layout we want to use.</li>
<li>Database: please setup here your database information.</li>
</ul>

<h2>Using the Zend Framework</h2>

<p>Download from <a href="http://framework.zend.com">the official Zend Framework site</a> the lastest version and copy all the library folder to the library folder of your application.</p>

<h2>Creating the bootstrap file</h2>

<p>Create an index.php file in the public folder with the following contents:</p>

<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<?php

/** Bootstrap */
/** Setting error reporting */
error_reporting(E_ALL | E_STRICT);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);

/** Setting paths */
$rootPath = dirname(dirname(_FILE_));
set_include_path(get_include_path() . PATH_SEPARATOR .
$rootPath . '/application/config' . PATH_SEPARATOR .
$rootPath . '/application/models' . PATH_SEPARATOR .
$rootPath . '/library' . PATH_SEPARATOR .
$rootPath . '/public');

/** Load application configuration ini file */
require_once 'Zend/Config/Ini.php';
$config = new Zend_Config_Ini('quickstart.ini', 'default');

/** Setup database */
require_once 'Zend/Db.php';
require_once 'Zend/Db/Table/Abstract.php';
$db = Zend_Db::factory($config->database);
Zend_Db_Table_Abstract::setDefaultAdapter($db);

/** Setup layout */
require_once 'Zend/Layout.php';
Zend_Layout::startMvc($config->appearance);

/** Setup front controller */
require_once 'Zend/Controller/Front.php';
$frontController = Zend_Controller_Front::getInstance();

$frontController->setControllerDirectory($rootPath . '/application/controllers')
->dispatch();
]]></ac:plain-text-body></ac:macro>

<p>This files setups the follows:</p>
<ul>
<li>Error reporting: this will allow you see errors (if any) in your application, please disable it in production environments.</li>
<li>Setting paths: this will add the paths needed by our application (configuration, models, ZF library and public).</li>
<li>Load configuration: this will load our configuration file.</li>
<li>Setup database: this will use our configuration file information in order to connect to the database.</li>
<li>Setup layout: this will configure our page layout.</li>
<li>Setup front controller: the front controller is the ZF component that does the MVC magic in the application.</li>
</ul>

<h2>Creating the comments model file (+ creating the write comments form)</h2>

<p>This file allows your application to interact with the database, the filename is CommentsTable.php and it must be in the models folder.</p>

<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<?php

require_once 'Zend/Db/Table/Abstract.php';

class CommentsTable extends Zend_Db_Table_Abstract
{
protected $_name = 'comments';
protected $_primary = 'comment_id';

/**

  • This function returns the form used for
  • adding comments in our comments table
    */
    public static function getAddCommentForm()
    Unknown macro: { require_once 'Zend/Form.php'; $form = new Zend_Form(array( 'method' => 'post', 'elements' => array( 'comments' => array('textarea', array( 'required' => true, 'label' => 'Write your comments' )), 'submit' => array('submit', array( 'label' => 'Send' )) ), )); return $form; }

/**

  • This function returns all the comments
  • in our comments database table
    */
    public static function getComments()
    Unknown macro: { $commentsTable = new CommentsTable(); return $commentsTable->fetchAll(); }

/**

  • This function creates a new row in our
  • comments database table with the data given
    */
    public static function createNewComments($data)
    Unknown macro: { $commentsTable = new CommentsTable(); $comment = $commentsTable->createRow($data); $comment->save(); }

    }
    ]]></ac:plain-text-body></ac:macro>

<p>This files includes:</p>
<ul>
<li>A require once to Zend/Db/Table/Abstract.php, this file is the ZF component that helps us to interact with our database comments table.</li>
<li>Two protected variables: $_name containing the database table name (in our case 'comments') and $_primary containing the primary key of the table.</li>
<li>getAddCommentForm method: this static method returns the form which our guests will need to write their comments.</li>
<li>getComments method: this static method returns all rows in the comments database table.</li>
<li>createNewComments method: this static method creates a new row in the comments database table.</li>
</ul>

<h2>Setting the main layout.</h2>

<p>The layout is the html that we want to show in every request done to our application, this will helps us to not repeat the same html code in all pages, this example will have 2 pages, the home page and an about page, but we are not going to create this files with the same boring html code, in order to not to repeat that, we create the main layout; to do this, create a file called main.phtml in the /application/layouts/scripts folder with the following content:</p>

<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Quickstart</title>
</head>
<body>
<h1>Quick start</h1>
<p>

<a href="<?php echo $this->baseUrl; ?>">Home</a>
<a href="<?php echo $this->baseUrl; ?>/about">About</a>

</p>
<?php echo $this->layout()->content; ?>
</body>
</html>
]]></ac:plain-text-body></ac:macro>

<p>This will create a title for our page and a little menu with two options (home and about). The line:</p>

<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<?php echo $this->layout()->content; ?>
]]></ac:plain-text-body></ac:macro>

<p>will output the real content that has been requested; in the following steps we will create that content.</p>

<h3>Creating the controllers</h3>

<p>The following files must be in the /application/controllers folder:</p>

<p>IndexController.php</p>
<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<?php

require_once 'Zend/Controller/Action.php';

class IndexController extends Zend_Controller_Action
{
public function init()

Unknown macro: { $this->view->baseUrl = $this->_request->getBaseUrl(); }

public function indexAction()
{
require_once 'CommentsTable.php';
$form = CommentsTable::getAddCommentForm();

if ($this->getRequest()->isPost()) {
if ($form->isValid($_POST))

Unknown macro: { $values = $form->getValues(); CommentsTable}

}

$this->view->form = $form;
$this->view->comments = CommentsTable::getComments();
}
}
]]></ac:plain-text-body></ac:macro>

<p>This controller includes two methods:</p>

<ul>
<li>init: this method will setup our base url (e.g. <a class="external-link" href="http://host/quickstart/">http://host/quickstart/</a> , this, is our base url, is the url where our application has been addressed)</li>
<li>indexAction: this will render our home page, notice that we are using the getAddCommentForm() method of the CommentsTable class, as well, we are adding the information given in the form to our comments table.</li>
</ul>

<p>AboutController.php</p>
<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<?php

require_once 'Zend/Controller/Action.php';

class AboutController extends Zend_Controller_Action
{
public function init()

public function indexAction()
{
}
}
]]></ac:plain-text-body></ac:macro>

<p>This controller will also use the baseUrl variable and an empty indexAction method.</p>

<p>ErrorController.php</p>
<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<?php

require_once 'Zend/Controller/Action.php';

class ErrorController extends Zend_Controller_Action
{
public function init()

Unknown macro: { $this->view->baseUrl = $this->_request->getBaseUrl(); }

public function errorAction()
{
}
}
]]></ac:plain-text-body></ac:macro>

<p>And finally we setup the error controller, any error found in our application will redirect to this controller/action.</p>

<h2>Creating the view files</h2>

<p>The view files are the html rendered code of each controller/action in our application. This files must be in the /application/views/scripts folder.</p>

<p>index/index.phtml</p>
<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<h2>Welcome to my application!</h2>
<p>Extending the art & spirit of PHP, Zend Framework is based on simplicity,
object-oriented best practices, corporate friendly licensing, and a rigorously
tested agile codebase. Zend Framework is focused on building more secure, reliable,
and modern Web 2.0 applications & web services, and consuming widely available APIs
from leading vendors like Google, Amazon, Yahoo!, Flickr, as well as API providers and
cataloguers like StrikeIron and ProgrammableWeb.</p>
<h3>Comments</h3>
<ul>
<?php foreach ($this->comments as $comment) :?>
<li><?php echo $this->escape($comment->comments); ?></li>
<?php endforeach; ?>
</ul>
<?php echo $this->form; ?>
]]></ac:plain-text-body></ac:macro>

<p>Notice that we are using the array $this->comments, this array has been passed to the view from the controller using:</p>

<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
$this->view->comments = CommentsTable::getComments();
]]></ac:plain-text-body></ac:macro>

<p>about/index.phtml</p>
<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<h2>About this application</h2>
<p>This application has been made using Zend Framework!</p>
]]></ac:plain-text-body></ac:macro>

<p>error/error.phtml</p>
<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<h2>Error</h2>
<p>An error has ocurred</p>
]]></ac:plain-text-body></ac:macro>

<p>And now we are done, just point your browser to <a class="external-link" href="http://host/quickstart">http://host/quickstart</a> and you are ready to see our application in action!</p>

<p><ac:image ac:thumbnail="true"><ri:attachment ri:filename="quickstart.JPG" /></ac:image><ac:image ac:thumbnail="true"><ri:attachment ri:filename="about.JPG" /></ac:image></p>

<h2>References</h2>

<ul>
<li>Default Project Structure <a class="external-link" href="http://framework.zend.com/wiki/x/6KM">http://framework.zend.com/wiki/x/6KM</a></li>
<li>Zend_Config <a class="external-link" href="http://framework.zend.com/manual/en/zend.config.html">http://framework.zend.com/manual/en/zend.config.html</a></li>
<li>Zend_Db <a class="external-link" href="http://framework.zend.com/manual/en/zend.db.html">http://framework.zend.com/manual/en/zend.db.html</a></li>
<li>Zend_Controller <a class="external-link" href="http://framework.zend.com/manual/en/zend.controller.html">http://framework.zend.com/manual/en/zend.controller.html</a></li>
<li>Zend_Layout <a class="external-link" href="http://framework.zend.com/manual/en/zend.layout.html">http://framework.zend.com/manual/en/zend.layout.html</a></li>
<li>Zend_Form <a class="external-link" href="http://framework.zend.com/manual/en/zend.form.html">http://framework.zend.com/manual/en/zend.form.html</a></li>
</ul>

Labels:
quickstart quickstart Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Mar 11, 2008

    <p>Hi,</p>

    <p>I suggest using Zend_Loader::registerAutoload() and then drop all the require_onces</p>

    <p>Regards,</p>

    <p>Rob...</p>