Zend Framework directory structure

Posted February 22nd, 2009 by Juozas

Currently I’m working on a new project which works on Zend Framework. I really like this framework and it has been my favourite for quite a long time – I especially like it’s standalone components and huge space for your personal programming style. But… I don’t know how to correctly implement modularity in Zend Framework.

I need to have modular systems, which would allow downloading module as an archive and installing it would be as hard as unpacking it. Modules should work in their own folders and don’t require any system modifications. Also, modules needs to have different controllers and views for different website parts: backend and frontend.

Zend Framework manual has examples for directory structures and suggests this modular structure:

application/
    *module_name*/
        controllers/
            IndexController.php
            FooController.php
        models/
        views/
            scripts/
                index/
                foo/
            helpers/
            filters/

But how to distinguish between frontend and backend parts? Some solutions suggests having backend as admin module, but modular structure would be lost – adding new modules would require modifying admin module. So I decided to invent my own directories tree and came up with:

application/
    *module_name*/
        controllers/
	    admin/
                 IndexController.php
                 FooController.php
	    public/
                 IndexController.php
                 FooController.php
        models/
        views/
            scripts/
                admin/
		     index/
		     foo/
                public/
		     index/
		     foo/
            helpers/
            filters/

It works perfectly (requires small bootstrap modifications), but I do not like it. What I don’t like is high depth of folders – working with files browser is really painful. Also, multiple folders share same admin and public folders, but it’s not really right – admin and public are parts of website and they should have different controllers, not controllers have different parts. So I reversed whole tree:

application/
    *module_name*/
        admin/
            controllers/
                 IndexController.php
                 FooController.php
            scripts/
                 index/
		 foo/
        public/
            controllers/
                 IndexController.php
                 FooController.php
            scripts/
                 index/
		 foo/
        models/
        views/
            helpers/
            filters/

I haven’t tried implementing it, but it works in theory. Having admin and public as root folders also creates possibility to add, for example, part dependant forms more easily.  However, I still don’t know which structure to choose – they all have different pros and cons, though last one seems to be most “natural”.

Which structure you use?

Comments (9)

  1. CoolKourt

    Hello, I like the last structure. It seems like even though there would be extra files and folders, maybe it would be easier for someone wanting/willing to learn such and advanced level of programming and start really building sites for clients tailored to their needs.

    Every quickstart I got for Zend didnt work.

    If you have any good links or resources to any Zend related projects I would love to see them.

    coolcourt@gmail.com

  2. Fozzy

    Hi,

    I’m racking my brain on this right now, too and I’m looking at it with regards to the new proposed project structure:

    Zend Framework Project Structure

    What I think I might have is:

    application/
    models/
    modules/

    controller/
    models/
    view/
    [...]

    As I see it, the “public” and “admin” section will be sharing the same models, such as a “user” or “account” model. There should be a application level “models” folder for shared resources across modules.

    Also, recall that a module *must* be within a context of an application. Sure, it can be a sort of stand-a-lone application, but it’s still part or “plugged-in” to the main application, meaning it should and can be safe to assume certain application level modules will be present to be allowed to be shared.

    That’s what I’m thinking at least. That might give you some ideas.

  3. Juozas (author)

    Hi, Fozzy,

    yes, module is part of application, eg. works only within it, not alone. But my idea is to develop application, which can accept new modules without changing other code – config files, language files, etc.

    I’ve seen proposed structure, but I din’t understood how they are going to deal with admin/public parts. Sure, you can add AdminController.php or have AdminIndex_Action() in controllers, but is this really a good way?

    My biggest headache is admin/public distinction. How you do it?

  4. Giorgio Sironi

    I do not agree on the admin/public distinction.
    I develop a CMS based on Zend Framework and Doctrine, and I use the modular structure from the manual. My solution define a Zend_Acl in a config/acl.ini file that allow an user with a certain role to execute some actions. Currently there are three levels: guest, user and admin, and they cannot be mapped on a admin/public structure. Access control, when done right, it is more than that.

  5. netors

    Juozas,

    I use the last structure,
    You just have to add this to your bootstrap.php

    	$frontController = Zend_Controller_Front::getInstance(); 
    	$frontController->throwExceptions(false); 
    	$frontController->setControllerDirectory(array('default'=>APP_DIR.'public'.DIRECTORY_SEPARATOR.'controllers','admin'=>APP_DIR.'admin'.DIRECTORY_SEPARATOR.'controllers'));

    Also, If you would like to add new modules, change the array passed to the setControllerDirectory (get creative, use a db or ini file etc.., make the admin module do that magic)

    I Hope it helps.

  6. Slack5

    admin/
    admin_application/
    module/
    controllers/
    views/
    application/
    module/
    controllers/
    views/
    doctrine/
    models/
    library/
    public/

  7. yann

    With ZF 1.7 I use same as your 2nd structure !

    Combined with apache ENV var and different subdomain for each module, it was great.

    I try to adapt it with ZF 1.9 but it’s a big mess.

    Did you run this structure ?

  8. aruss

    Hi, did your solution work? how do you implemented it? would you share some parts of your code?

    I try a solution with two applications like descibed in symphony docu http://www.symfony-project.org/book/1_2/02-Exploring-Symfony-s-Code#chapter_02_code_organization

    I think it is cleaner solution, at the end you have three diretories frontend backend modules and model directory for the library

    greets
    aruss

  9. Aryashree Pritikrishna

    Hi…

    Nice article one, about creating admin modules.

    Thanks,
    Arya

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">