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

Related posts:

  1. Zend Framework – quick tutorial (part 3) – front controller plugins
  2. Zend Framework – quick tutorial (part 1)
  3. Setting Up Zend Framework with Modules
This entry was posted in featured, web development, zend framework and tagged , , , , , , , . Bookmark the permalink.

8 Responses to Zend Framework – quick tutorial (part 2)

  1. Jyotsna says:

    Hi,

    Thank you very much for this information. It helped me a lot.

    Thanks again,
    Jyotsna.

  2. Good article, adding it to my bookmarks!

  3. Great article, adding it to my bookmarks!

  4. Just read some other comments on your blog, and I agree with the general impression, your doing a great job!

  5. Pedrito says:

    Your writing style is quite a good role model for me – I have recently started my own blog and I am really struggling to write articles!

  6. InfoSeek says:

    I was wondering if you could set up some sort of system so when your publish a new article, i get emailed to alert me? Or something like that.

  7. Crispijn says:

    I’m new with Zend after several years of programming php and several classes. I’m having troubles setting up Zend Framework 1.10.0 on my local machine. Has there been some changes in the set up or am I doing things wrong? I’ve set up the folder structure just like the structure on this post but I’ll get the following message:

    Notice: Zend_Loader::Zend_Loader::registerAutoload is deprecated as of 1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader instead in /Users/crispijn/Sites/Zend/Loader.php on line 223

    Fatal error: Uncaught exception ‘Zend_Controller_Dispatcher_Exception’ with message ‘Invalid controller specified (index)’ in /Users/crispijn/Sites/Zend/Controller/Dispatcher/Standard.php:242 Stack trace: #0 /Users/crispijn/Sites/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 /Users/crispijn/Sites/bootstrap.php(20): Zend_Controller_Front->dispatch() #2 /Users/crispijn/Sites/index.php(10): require(‘/Users/crispijn…’) #3 {main} thrown in /Users/crispijn/Sites/Zend/Controller/Dispatcher/Standard.php on line 242

    Maybe it’s time to update your post and make us happy again! I looked arround your site and I think it’s great to have such a great resource for a framework! Keep up the good work!

  8. Stoimen says:

    Actually that post was a bit earlier than the 1.8 version of Zend Framework where Zend_Loader_Autoloader was introduced. In fact I described that in another post here. Thanks for the advice I’ll check my source again!

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=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">