Create Multiple Websites with One Shared Library by Zend Framework

When we try to generate the project by Zend_Tool by command line like this:

$ zf create project myProject

 

We get a standard structure which is recommended by Zend Framework:

It works very fine as “one” website project. But how about if we want to create multiple websites without rewriting too many code?
The main idea is to share as much as possible the libraries meanwhile keeping independence of each website, especially the designs/templates/publics.

 


 

Here is one simple way which makes the minimal modifications and satisfy our needs:

 

(1) Move the folder “library” out of the project.

 

(2) Modify /public/index.php as below to make sure the library/ is in include paths:

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    //realpath(APPLICATION_PATH . '/../library'),
    realpath(APPLICATION_PATH . '/../../library'),
    get_include_path(),
)));

 

(3) Modify /application/config/application.ini :

;includePaths.library = APPLICATION_PATH "/../library"
includePaths.library = APPLICATION_PATH "/../../library"

 


 

Assume that we have project1, project2 and project3, all applied the same treatment. And finally the websites could like this:

In the picture above, assume that our global namespace is “Projlib”. We have a model class Projlib/Model/Foo.php

class Projlib_Model_Foo
{
    // ...
}

 

Then we can just put this configuration inside application/configs/application.ini to make it autoloaded:

; --- my library prefix -----------------------------------------------------------------
autoloadernamespaces.global = "Projlib_"

 
 
And that’s all! Enjoy it.

Posted in Zend Framework | Tagged | 1 Comment
  1. Thanks for a good idea!:d