How to override/clone modules, which are downloaded to the vendor by composer in zf2?

前端 未结 2 2037
-上瘾入骨i
-上瘾入骨i 2021-01-27 07:34

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/vi

2条回答
  •  天涯浪人
    2021-01-27 07:45

    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

提交回复
热议问题