在多站点CakePHP 2.3安装中共享控制器,模型和视图 [英] Share controllers, models and views in multisite CakePHP 2.3 installation

查看:63
本文介绍了在多站点CakePHP 2.3安装中共享控制器,模型和视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建CakePHP 2.3高级安装(几个应用程序-网站-共享一个相同的 lib 文件夹,所有cakes的核心文件都位于该文件夹中)。这项工作没有任何问题,我只需要为每个应用编辑Config文件夹中的 core.php 文件,以便它知道在哪里可以找到蛋糕的文件。文件系统如下所示:

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.

我已经在CakePHP中看到了供应商插件的概念(我实际上在那些网站中使用了插件,但是从/ app / plugins文件夹),但我不确定这是否适合我的情况,或者我将如何设置。我想这个想法是在[cake-core-files]和[websites]的同一级别上有另一个文件夹(例如[shared_objects]),但是我不知道如何配置Cake才能做到这一点或如何从每个应用程序调用这些对象。

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.

有任何想法吗?

编辑 >

基于下面的评论/响应,我正在尝试使用bootstrap.php中的App:build()函数执行此操作,但是我无法获取它起作用。这是我所做的:

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. 添加了一个新文件夹,我希望将其放置在所有应用程序之间共享:

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

[root]
[cake-core-files]
[shared-stuff]->新文件夹
[Model]
[配置]
[网站]
[网站-1]
等...

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

已放置新文件夹 [共享的东西/模型]

添加了此内容引导程序中的行:

Added this line in the bootstrap:

App::build(array('Model' => array('/path/to/shared-stuff/Model')));


  • 从其原始位置删除模型 User.php [website-1 / app / Model]

    在此步骤之后,它似乎可以正常工作,则可以从 [shared-stuff] 文件夹正确加载模型User.php(我已经测试了打印 App :: objects('Model'); 列出了用户模型)。但是,它实际上不起作用,当我从login()函数中的 UsersController 调用该模型时,登录不起作用(尽管我什至没有收到任何类型的错误,并将调试设置为2)。

    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).

    此模型使用的数据库配置与默认数据库配置不同( users 表位于其他数据库中)比默认值)。我不知道这是否重要。

    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.

    可以肯定的是,如果我在其原始位置保留相同的User.php模型( [网站-1 / app / Model] ),包括登录名在内,一切都可以正常工作,所以这是我尝试配置所有共享内容而不是模型本身的问题。

    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?

    推荐答案

    我认为共享一些控制器/在多个网站之间建立模型,并且无需插件即可执行:使用共享的控制器/模型可让您在需要时将其覆盖。只需将控制器/模型复制到网站的正确文件夹中,系统就应该使用它而不是共享文件夹!

    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!

    编辑:很好,它可以工作,但是我认为Cake的官方文档中有一个小错误:所有路径都应以目录分隔符终止!在cakephp书中,没有斜杠。将其添加到您的App :: build路径,一切将正常运行!

    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!

    这篇关于在多站点CakePHP 2.3安装中共享控制器,模型和视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆