Zend framework

From EKiniWiki

Jump to: navigation, search
Zend Framework Logo
Zend Framework Logo

An overview of the ZendFramework: http://framework.zend.com

The directory structure is based on this best-practice structure found on the Zend Framework Manual: http://framework.zend.com/manual/en/zend.controller.html#zend.controller.quickstart

It has evolved into this with the help of the following:


Contents

[edit] Directory Structure

Here is the basic modular directory structure:

Image:Modular_directory_structure.png

[edit] The .htaccess Files

This file will go into /zend_quickstart/html/.htaccess - beside the index.php (bootstrap file).

RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php

[edit] Basic Bootstrap File

This is /zend_quickstart/html/index.php.

The directory structure (see above) will need updating to include new "namespaces" - in this case "Naneau" for the sample Plugins and Action Helper.

<?php
error_reporting(E_ALL|E_STRICT); 
set_include_path('../application/library');
require_once('../application/library/Zend/Loader.php');
Zend_Loader::registerAutoload();

// Given an array of configuration data 
$configArray = array( 
    'type'     => 'PDO_MYSQL', 
    'database' => array( 
        'host'     => 'localhost', 
        'username' => 'root', 
        'password' => 'root', 
        'dbname'   => 'angel' 
    ) 
); 

$db = Zend_Db::factory($configArray['type'], $configArray['database']);
Zend_Db_Table::setDefaultAdapter($db);
Zend_Registry::set('dbAdapter', $db);

$frontController = Zend_Controller_Front::getInstance();
$frontController->addModuleDirectory('../application/modules/');

//Set up views
$view = new Zend_View();
$view->baseUrl = $frontController->getBaseUrl();

//set the base url
$view->addHelperPath('../application/modules/default/views/helpers', 'My_View_Helper');
//view configured

$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView($view);

//make view renderer use the view we just configured
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);

//register the Action Helpers
Zend_Controller_Action_HelperBroker::addPrefix('Naneau_Controller_Action_Helper'); 
// Location: zend_quicksrtart/application/library/Naneau/Controller/Action/Helper
// _'s are converted to /'s

//a sample plugin. naneau says that you have to register plugins in the bootsrap
$timeStart = microtime(true);
$frontController->setParam('timeStart', $timeStart);
$frontController->registerPlugin(new Naneau_Controller_Plugin_ExecutionTime());

//set view renderer in the action helper broker
$frontController->dispatch();

//enjoy!!!

[edit] Handling Variables

[edit] Posted Variables

How to get variables from POST Method:

$customer_name = $this->_request->getPost('customer_name');

Although it would be better if you do this:

$filter = new Zend_Filter_StripTags();
$data['news_content'] = $filter->filter($this->_request->getPost('news_content'));

[edit] Variables in the URL

How to get variables from GET Method:

//http://site/controller/action/id/1/name/gerry
//$this->view->newsid = $this->_getParam('id', false); 
$this->view->newsid = $this->getRequest()->getParam('id');

[edit] Checking for POST Request

You can check if there are variables POSTED from the previous request using this:

if (!$this->_request->isPost()) {
    throw new Exception('Invalid Request.');
}

[edit] Set Headers for Pages

You can set headers sent for pages using the following code:

$this->getResponse()->setHttpResponseCode(403);


[edit] Set Module or Controller or Action

You can set the Module/Controller/Action. For intance, in this case, the code sends a 403 error and then redirects the page to default/auth/noacess (NOTE: that default module is not actually displayed - since it is the default)

$this->getResponse()->setHttpResponseCode(403);
$request->setModuleName('default');
$request->setControllerName('auth');
$request->setActionName('noaccess');

[edit] Downloads

You can download the Zend Quickstart Modular Modular Directory structure here: Media:Zend_quickstart.tar.gz

Just untar in your public_html and redirect your browser to something like:

  http://localhost/zend_quickstart/html
Personal tools
Bookmarks