Tag Archives: quick start

Zend Framework – quick tutorial (part 3) – front controller plugins

Why writing a front controller plugin?

Almost every application uses a database connection and acl module. Why doing this in the bootstrap and to mantain many lines of code there, instead of making it clear and mantainable. Of course you can have all these lines of code in your bootstrap, but you know for serious applications that recently will become an obstacle. That’s why Zend Framework allows you to use Front_Controller plugin.

First in you bootstrap add those lines of code

/*
 * add a simple plugin to the controller
 */
 
$front->registerPlugin(new Zend_Controller_Plugin_Init())
      ->registerPlugin(new Zend_Controller_Plugin_Acl());

These two classes (Zend_Controller_Plugin_Init and Zend_Controller_Plugin_Acl) should be placed in two different files with the same names in Controller/Plugins directory under Zend folder, and garantee you that both classes will be instanciated before starting the front controller. Continue reading Zend Framework – quick tutorial (part 3) – front controller plugins

Zend Framework – quick tutorial (part 2)

Directory Layout and Bootstrapping.

When someone starts with the learning of a framework, first he begins to read various articles to understand the basic rules to work with.

There are a lot of tutorials how exactly to start, and there is also an official quick start guide, but beside this there are too much advices how should you directory layout look like.

For me the following directory layout is working well. I don’t remember exactly where I’ve found it, but I think many people use it.

  • application
    • admin
    • controllers
    • models
    • views
  • content
    • controllers
    • models
    • views
  • layouts
  • bootstrap.php
  • config.ini
  • library
  • Zend
  • public
  • index.php

Almost half of the sources “how to start” recommend all of the initialization should be in index.php in the public folder. In fact all the logic of the public/private folders is that we don’t like to show anything on the web, and that’s why index.php contains only the following peace of code (it’s recommended to leave the file open from the ?> closing tag):
<?php
require '../application/bootstrap.php';

Anything else, all the initialization is in bootstrap.php:

error_reporting (E_ALL | E_STRICT);

ini_set (‘display_startup_errors’, 1);
ini_set (‘display_errors’, 1);

set_include_path (‘../library’ . PATH_SEPARATOR . get_include_path());

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

$front = Zend_Controller_Front::getInstance();

$front->throwExceptions(true);

$front->setControllerDirectory(array(‘default’ => ‘../application/content/controllers’,
‘admin’   => ‘../application/admin/controllers’));

$front->dispatch();
We set the error reporting to be ON, which si good when you develop any application and to collect all error messages. Than add the library folder in the include path with that line of code:

set_include_path(‘../library’ . PATH_SEPARATOR . get_include_path());

Next step is to start the __autoload functionality built in Zend Framework:

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

Finally start the front controller which implements the singleton pattern and point to the two main directories – admin and content. I used them for administration panel and front end, but you can used as many as you application needs:
$front->setControllerDirectory(array('default' => '../application/content/controllers',
'admin'   => '../application/admin/controllers'));

Related Links

Zend_Db tutorial