Share controllers, models and views in multisite CakePHP 2.3 installation

不羁的心 提交于 2019-12-12 01:34:19

问题


I'm creating a CakePHP 2.3 advanced installation (several apps -websites- that share one same lib folder where all of cake's core files are located). This works without any problems, I just edit the core.php file in the Config folder for each app so it knows where to find cake's files. The file system looks something like:

[root]
    [cake-core-files]
    [websites]
        [website-1]
            [app]
            [plugins]
            [vendors]
        [website-2]
        ...
        [website-N]

These different apps are in fact different in some things (they are different websites) but at the same time there's many things that are common to all of them (for example some models, controllers, functions...). What I would like to do, if possible, is to have those apps also share a bunch of controllers, models, etc so I can put them in one place, instead of replicating them now for each app.

I've seen the concept of vendors and plugins in CakePHP (I actually use plugins in those websites, but from the /app/plugins folder), but I'm not sure if that would work in my case, or how I would set that up. I guess the idea would be to have another folder (for example [shared_objects]) at the same level of [cake-core-files] and [websites], but I don't know how I would have to configure cake to do that or how to call those objects from each app.

Any ideas?

EDIT

Based on the comments/responses below I'm trying to do this using the App:build() function in the bootstrap.php, but I can't get it to work. This is what I've done:

  1. Added a new folder where I want to put the stuff to share between all apps:

    [root] [cake-core-files] [shared-stuff] --> NEW FOLDER [Model] [Config] [websites] [website-1] etc...

  2. Placed the model User.php inside the new folder [shared-stuff/Model]

  3. Added this line in the bootstrap:

    App::build(array('Model' => array('/path/to/shared-stuff/Model')));
    
  4. Deleted the model User.php from its original location [website-1/app/Model]

After this steps, it seems to work, the model User.php is loaded correctly from the [shared-stuff] folder (I've tested that printing App::objects('Model');, the User model is listed). However, it actually doesn't work, when I call that model from the UsersController in the login() function, the login doesn't work (although I don't receive any kind of error, even with debug set to 2).

This model uses a database configuration different from the default one (the users table is located in a different database than the default one). I don't know if this matters.

One thing is for sure, if I leave the same exact User.php model in its original location ( [website-1/app/Model]) it all works fine, including the login, so it's a problem with how I try to configure all this sharing stuff, not the model itself.

Any ideas what am I doing wrong?


回答1:


I think it could be useful to share some controller/model between multiple websites, and do it without a plugin: Using shared controller/model lets you to overwrite it if needed. It should happen simply copying the controller/model in the website's correct folder and the system should use it instead of the shared one!

EDIT: Wonderful, it works, but i think there is a little error in cake's official documentation: All paths should be terminated with a Directory separator! In the cakephp book there aren't trailing slash. Add it to your App::build path and everything will work perfectly!




回答2:


You can have plugins in the core Plugins/ dir

[root]
    [lib]
        [Cake]
    [Plugins]
        [Available]
        [To]
        [All]
    [website-1] // the 'app' dir -> https://github.com/cakephp/cakephp/tree/master/app
        [plugins]
        [vendors]
    [website-2] // you can have many of them named anything, 'app' is just one.
        ...
    [website-N]

This folder specifically will make the plugins available to any app using the cake lib

EG:

Look at this. Copy app to website-1, repeat till website-n.



来源:https://stackoverflow.com/questions/14126587/share-controllers-models-and-views-in-multisite-cakephp-2-3-installation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!