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:

[codesyntax lang=”bash” lines=”no”]

$ zf create project myProject

[/codesyntax]

 

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:

[codesyntax lang=”php” lines=”no”]

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

[/codesyntax]

 

(3) Modify /application/config/application.ini :
[codesyntax lang=”ini” lines=”no”]

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

[/codesyntax]

 


 

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
[codesyntax lang=”php” lines=”no”]

class Projlib_Model_Foo
{
    // ...
}

[/codesyntax]

 

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

[codesyntax lang=”ini” lines=”no”]

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

[/codesyntax]

 
 
And that’s all! Enjoy it.

Posted in Zend Framework | Tagged | 1 Comment