cakephpvendors

CakePHP multi-site file system organization with shared functions


I have a CakePHP 1.3 mutli-site installation in the server. Each site has its own folder, and they all load the CakePHP core libraries from one same place. This is how it looks like:

I find myself many times in the situation where I want to implement some functions that I would like to use in all of the apps, but so far I just replicate the code inside each site. I would like to be able to declare those functions in one place and be able to call them from any site. I believe vendors could have something to do with this, but I've been looking in the cookbook and I can't figure out how to use that in my case.

Any help would be much appreciated!

UPATE

So by looking at the vendors, it seems to me (maybe I'm wrong) that it would work in my case if the functions in those vendors files don't need to directly interact with other CakePHP classes (for example Session, Auth, etc). I'll give an example of one of the things I'm trying to do, maybe that helps to understand better what I'm trying.

Each of the sites has its own login system (using cake's Auth). One of the things I want to do is, when a user logs in (the login() function in UsersController is called), have there a function that does some processing with the username of the user, and then calls the Session->flash() function to insert there a message (the message changes depending on that processing the function makes). So this function needs to use the User model and the Session model.

Can I do this using vendors, so I declare this one function (i.e. processLogin()) in a vendor, and all the sites can use that function? If so, how? If not, is there any other way to do this?


Solution

  • Stick them in the vendor folder at the same level of cake and sites, name-spaced under a name of your choice (usually the agency name or other unique identifier).

    Examples on loading vendor files in CakePHP 1.3 is explained in the Cookbook here: http://book.cakephp.org/1.3/en/view/943/Loading-Vendor-Files

    However, I’d advise upgrading to CakePHP 2, and placing your code in a sub-folder under lib.