Zend Framework - 覆盖模块

发布于 2024-11-30 15:28:10 字数 3860 浏览 3 评论 0原文


我正在 Zend Framework 中开发多网站 CMS。
我已经到了需要覆盖网站/应用程序文件夹中的应用程序/文件夹中的模块的地步。
对我的问题有更好的解释:
这是我的应用程序的树(重要部分):

library/
application/
        module1/
            controllers/
            models/
            ....
        module2/
            controllers/
            models/
            ....
websites/
  website1.com/
       application/
            module1/
                controllers/
                models/
                ....

所以我需要做的是网站/website1.com/application/中的 module1/ 覆盖应用程序中的 module1/(如果它存在)。我希望网站文件夹中 module1/ 中的所有内容覆盖主应用程序文件夹中的所有内容。
我还希望此 module1 中是否有 2 个控制器(例如 IndexController 和 TestController),并且如果我仅将 TestController 放在 module1/controllers 下的网站文件夹中,以仅覆盖应用程序文件夹中的 TestController 并从主文件夹中获取 IndexController。
抱歉,如果我未能准确解释我想要实现的目标。有什么不清楚的地方请追问。
谢谢。

编辑: 好的,首先 - 感谢您的评论。
拥有网站/文件夹的原因主要是因为虚拟主机,因为我更喜欢我的所有网站都有单独的(公共?)文件夹,而拥有一个带有应用程序文件夹的库的原因显然是因为升级原因(所以当我,例如,升级 Zend - 我不需要为每个网站都升级它)。
我很可能很少使用控制器的重写选项,是的,我什至更喜欢如果我可以,例如,扩展主控制器(例如 - IndexController)并重写一些函数,但我认为这比重写整个要困难得多类。
这是我的应用程序的完整结构:

 library/ - Library folder contains Zend and many other classes that I'll use in my application.
     Zend - Zend Framework
     MyCMS - Classes from my old CMS.
 sites/ - Folder that contains websties.
     website_1 - Website one.
         application/ - Application folder for website one. If I need to redefine module or something. So, if I need to override module: main_module, I'll  create folder main_module here with files that I want to override.
         config/ - Configuration for website_1 - if I need to override, for example, application.ini
         lang/ - Language files for this specific website.
         templates/ - Templates folder for website (layouts and templates). By the way, I'm using smarty.
             default/ - Main template.
                 layout/ - Layouts for Zend View.
                 css/
                 js/
                 images/
                 modules/
         files/ - Place to upload files in, for this website. This will contain user avatars and stuff.
         index.php - Main file that runs bootstrap and application.
         Bootstrap.php - Inherited bootstrap. In case I need to override some functions from default bootstrap.
 application/ - Main folder that contains application modules and stuff.
    main_module/
         configs/ - Module configuration.
             config.ini
         controllers/ - Controllers for this module.
         modules/ - Submodules. There are like boxes that I display on website. For example, if my main module is "news", here, I'll make new sub-module to display box with statistics.
             submodule/
                 services/ - XML/JSON/whatever service. If someone targets controller in services with specific parametars, it'll return response in requested format.
                     controllers/ - Services will only have controllers.
                 configs/ - Configuration for this submodule.
                 controllers/ - Controllers for this submodule.
                 models/ - Models for this submodule.
                 lang/ - Language files for this submodule.
                 template/ - Templates for this submodule.
                     helpers/
                     css/
                     js/
                     images/
                     index.html
         models/ - Models for main module.
         lang/
         services/  - Main module will also have services. See submodule services for explanation.
             controllers/
         template/
             helpers/
             css/
             js/
             images/
             index.html
     Bootstrap.php  - This is main bootstrap file that every website's bootstrap file will extend (and override some methods - if needed).

I'm working on multi-website CMS in Zend Framework.
I've came to a point where I need to override module from application/ folder in my website/application folder.
A bit better explanation of my issue:
Here's tree of my application (important part):

library/
application/
        module1/
            controllers/
            models/
            ....
        module2/
            controllers/
            models/
            ....
websites/
  website1.com/
       application/
            module1/
                controllers/
                models/
                ....

So what I need to do is that module1/ in websites/website1.com/application/ override module1/ in application, IF it exists. I want everything in module1/ in websites folder to override everything in main application folder.
I'd also like if there are 2 controllers in this module1 (for example IndexController and TestController) and if I put only TestController in websites folder under module1/controllers to override ONLY TestController from Application folder and to get IndexController from main folder.
Sorry if I failed to explain exactly what I'm trying to achieve. If there's something unclear, please ask.
Thank you.

Edit:
Okey, first of all - thanks for your comments.
Reason for having websites/ folder is, mostly because of vhost as I prefer that all of my websites have separate (public?) folders, and reason for having one library with application folder is because, obviously, upgrade reasons (so when I, for example, upgrade Zend - I don't need to upgrade it for every website).
I'll most likely rarely use overriding option for controllers, and yes, I'd even prefer if I could, for example, extend main Controller (for example - IndexController) and override some functions, but I thought that's way harder then override whole class.
Here's full structure of my application:

 library/ - Library folder contains Zend and many other classes that I'll use in my application.
     Zend - Zend Framework
     MyCMS - Classes from my old CMS.
 sites/ - Folder that contains websties.
     website_1 - Website one.
         application/ - Application folder for website one. If I need to redefine module or something. So, if I need to override module: main_module, I'll  create folder main_module here with files that I want to override.
         config/ - Configuration for website_1 - if I need to override, for example, application.ini
         lang/ - Language files for this specific website.
         templates/ - Templates folder for website (layouts and templates). By the way, I'm using smarty.
             default/ - Main template.
                 layout/ - Layouts for Zend View.
                 css/
                 js/
                 images/
                 modules/
         files/ - Place to upload files in, for this website. This will contain user avatars and stuff.
         index.php - Main file that runs bootstrap and application.
         Bootstrap.php - Inherited bootstrap. In case I need to override some functions from default bootstrap.
 application/ - Main folder that contains application modules and stuff.
    main_module/
         configs/ - Module configuration.
             config.ini
         controllers/ - Controllers for this module.
         modules/ - Submodules. There are like boxes that I display on website. For example, if my main module is "news", here, I'll make new sub-module to display box with statistics.
             submodule/
                 services/ - XML/JSON/whatever service. If someone targets controller in services with specific parametars, it'll return response in requested format.
                     controllers/ - Services will only have controllers.
                 configs/ - Configuration for this submodule.
                 controllers/ - Controllers for this submodule.
                 models/ - Models for this submodule.
                 lang/ - Language files for this submodule.
                 template/ - Templates for this submodule.
                     helpers/
                     css/
                     js/
                     images/
                     index.html
         models/ - Models for main module.
         lang/
         services/  - Main module will also have services. See submodule services for explanation.
             controllers/
         template/
             helpers/
             css/
             js/
             images/
             index.html
     Bootstrap.php  - This is main bootstrap file that every website's bootstrap file will extend (and override some methods - if needed).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ可否回来 2024-12-07 15:28:10

更新

尽管我强烈反对您的目录结构,但它是您的应用程序,您应该按照您喜欢的方式构建它。

要加载多个控制器,您应该创建一个前端控制器插件来添加控制器目录堆栈的路径

class My_Controller_Plugin_Paths extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch()
    {
        // Something like this...
        // Would be best to load paths via config/database or somewhere
        $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
        $dispatcher->addControllerDirectory('/path/to/website1.com/controllers')
                   ->addControllerDirectory('/path/to/website2.com/controllers');
    }
}

这完全未经测试,但您明白了。只要确保在引导程序中使用前端控制器注册插件即可,


我同意@Laykes。这是一个结构很糟糕的应用程序。

我不确定您的确切要求,但如果这是我的应用程序,我会尝试像这样构建它:

/application
    /modules
        /default
            /controllers
                /IntexController.php      // Default_IndexController
                /Website1com
                    /IndexController.php  // Default_Website1com_IndexController (possibly extends Default_IndexController)

在这里您可以看到结构正确的类继承,而无需创建完全独立且可能重复的应用程序文件夹。

自动加载类似的东西完全取决于你和你的优先事项。你可以做很多事情,每件事都有自己的+ve和-ve。

  • 您可以按照您想要的顺序将所有路径放入 include_paths
  • 检查文件是否存在并从前端控制器插件加载

仅举几个例子。

Update

Even though I highly discourage your directory structure, it's your application and you should structure it however you like.

To load multiple controllers, you should create a Front Controller Plugin to add paths to the controller directories stack

class My_Controller_Plugin_Paths extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch()
    {
        // Something like this...
        // Would be best to load paths via config/database or somewhere
        $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
        $dispatcher->addControllerDirectory('/path/to/website1.com/controllers')
                   ->addControllerDirectory('/path/to/website2.com/controllers');
    }
}

This is entirely untested, but you get the idea. Just make sure you register the plugin with the Front Controller in you bootstrap


I'd agree with @Laykes. This is a badly structured application.

I'm not sure of your exact requirements, but if it were my application, I would try to structure it like this:

/application
    /modules
        /default
            /controllers
                /IntexController.php      // Default_IndexController
                /Website1com
                    /IndexController.php  // Default_Website1com_IndexController (possibly extends Default_IndexController)

Here you can see a properly structured class inheritance without creating totally separate, and probably duplicate, application folder.

Autoloading anything like this though totally depends on you and your priorities. You could do a number of things, each with their own +ve's and -ve's.

  • You could throw all paths into your include_paths in the order you want
  • Check files exist and load from a front controller plugin

To name a couple.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文