如何覆盖/克隆在zf2中由composer下载到供应商的模块? [英] How to override/clone modules, which are downloaded to the vendor by composer in zf2?

查看:75
本文介绍了如何覆盖/克隆在zf2中由composer下载到供应商的模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SO !!!

我在基于Zend Framework 2. *的项目中添加了zfcUser模块(但我也想与其他模块一起使用).我如何覆盖此模块的控制器/视图/配置,因此不会通过使用composer更新依赖项来删除对它们的更改(我试图这样做并且未发现文件被更改,但是如果...,该怎么办? ).

我知道可以这样做,但是我不知道.查看助手?要不然是啥?

请帮帮我!谢谢.

其他信息

ZfcUserOverride的当前文件夹和文件:

解决方案

让我们一一回答所有问题.

但在此之前,请记住,ModuleManager正在创建应用程序配置",以合并所有已注册模块的所有模块服务配置(每个模块中的默认文件名为config/module.config.php)(输入"modules"您的application.config.php)从第一个到最后一个开始:这意味着最后一个获胜".

举一个实际的例子,如果在模块"A"中有一个名为"XYZ"的配置键来覆盖其在模块"B"中的值,则必须在"A"之后注册模块"B".

假设您要在应用程序模块中覆盖ZfcUser配置,则后者必须在前者之后注册.

'modules' => array(
    'ZfcUser',
    'Application',
),

1)如何仅覆盖 查看脚本?

如上所述,您应该创建一个自定义模块(建议您使用zftool来创建新的项目和模块支架),其中应包含您在views/{module name}文件夹(views/zfc-user)中覆盖的自定义视图脚本ZfcUser模块). 简单尝试一下,创建module/{module-name}/view/zfc-user/user/login.phtml文件,其中包含任何内容,请访问/user/login网址:您应该看到覆盖的视图脚本. /p>

2)如何覆盖控制器?

控制器像服务一样注册,如果您看到ZfcUser模块配置,就可以找到

'controllers' => array(
    'invokables' => array(
        'zfcuser' => 'ZfcUser\Controller\UserController',
    ),
),

'zfcuser'键可用于路由配置

                'defaults' => array(
                    'controller' => 'zfcuser', // this refers to the controller service
                    'action'     => 'index',
                ),

在ZfcUser模块中,用户控制器,路由规则和template_path_stack具有相同的名称"zfcuser",乍一看可能会造成混淆,只需记住它们所指的是不同的服务(视图,控制器和路由配置).

要强制应用程序使用自定义控制器而不是ZfcUser的UserController,您只需要在自定义模块中覆盖controllers服务,例如follow(对于本示例,我使用了Application模块)

'controllers' => array(
    'invokables' => array(
        'zfcuser' => 'Application\Controller\UserController',
    ),
),

您的"Application \ Controller \ UserController"可以扩展原始控制器,以保留原始功能(也许您只需要编辑几个方法).

3)如何覆盖配置?

如果对于"config",您的意思是 service 配置与第二点所做的工作相同:取配置密钥并更改其值.

ZfcUser是一个可配置的模块,这意味着可以配置一些值,任何类型的值(不一定是服务).看一下config/zfcuser.global.php.dist:此文件(没有重命名.dist后缀)可以包含在{Application}/config/autoload目录中,框架将自动加载其内容(该文件将模块). 例如,您可以配置哪些身份字段,是否使用验证码,登录后路由等等...

在这里我将不解释如何制作可配置的模块,有关如何执行此模块的教程很多.​​

我希望我没有犯任何错误,如果您有任何问题,请回答:)

欢呼

SO!!!

I added zfcUser module (but I want to do this with any other module too) to my project, based on Zend Framework 2.*. How I can override controllers/views/configs of this module, so changes of them wouldn't be deleted with updating dependencies with composer (I tried to do this and didn't found files are chaned, but what if.... Oo).

I know that there are the way existing to do this, but I don't know it. View Helper? Or what?

Help me, please! Thank you.

ADDITIONAL INFO

Current folders and files of ZfcUserOverride:

解决方案

let's answer all your question one by one.

But before let's keep in mind that the ModuleManager is creating the "Application configuration" merging all the modules service configurations (default filename is config/module.config.php within each module) of all registered modules ('modules' key in your application.config.php) starting from the first to the last: this means that the last "wins".

To make a practical example, if you have a configuration key called "XYZ" in module "A" to override its values in module "B", the module "B" must be registered after "A".

Assuming that you want to override ZfcUser configs in your Application module the latter must be registered after the former.

'modules' => array(
    'ZfcUser',
    'Application',
),

1) How to override only view scripts?

As already mentioned, you should create a custom module (I suggest you to use zftool to create new project and module scaffolding) that contains your override custom view scripts inside the views/{module name} folder (views/zfc-user for ZfcUser module). Give it a simple try creating the module/{module-name}/view/zfc-user/user/login.phtml file with anything inside it, the visit the /user/login url: you should see your overridden view script.

2) How can I override a controller?

Controllers are registered like services, if you see ZfcUser module config you can find

'controllers' => array(
    'invokables' => array(
        'zfcuser' => 'ZfcUser\Controller\UserController',
    ),
),

The 'zfcuser' key can be used in the route configuration

                'defaults' => array(
                    'controller' => 'zfcuser', // this refers to the controller service
                    'action'     => 'index',
                ),

In ZfcUser module the user controller, routing rules and template_path_stack have the same name, 'zfcuser' and at first glance it can be confusing, just keep in mind that they refer to different services (view, controller and route config).

To force the application to use your custom controller instead of the ZfcUser's UserController you only need to override the controllers service in your custom module like follow ( for this example I used the Application module)

'controllers' => array(
    'invokables' => array(
        'zfcuser' => 'Application\Controller\UserController',
    ),
),

Your 'Application\Controller\UserController' can extend the original controller in order to keep the original functionality (perhaps you need to edit only a couple of methods).

3) How can I override a config?

If for 'config' you mean a service config just do the same work as point 2: take the configuration key and change its value.

ZfcUser is a configurable module, this means that there are values, any kind of values not necessarily services, that can be configured. Take a look of the config/zfcuser.global.php.dist: this file, renamed without the .dist suffix, can be included in your {Application}/config/autoload directory and the framework will automatically load its content (that will configure the module). For example you can configure which are the identity fields, whether to use or not captcha, route after login and so on...

I will not explain here how to make a configurable module, there are plenty of tutorial on how to do it.

I hope I didn't mistake anything and that I have answered in a comprehensive manner to your questions, if you have problem just reply :)

Cheers

这篇关于如何覆盖/克隆在zf2中由composer下载到供应商的模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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