Question

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:

Current folders and files of ZfcUserOverride

Was it helpful?

Solution

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

OTHER TIPS

The best way to do this would be to create your own module to override everything you want to override. The views is easy, just add a subfolder called zfc-user in your module's view folder with a subfolder user, so you get YourModule/view/zfc-user/user In this custom views folder you can simply add your own versions of the ZfcUser templates: login.phtml, register.phtml etc.

For this to work make sure your module is loaded after ZfcUser though. So in your config/application.config.php make sure YourModule comes after ZfcUser in the 'modules' array.

To override the controllers just define your own UserController in your own module, either extending ZfcUsers's UserController or defining your own. Then have the routing take care of the rest. So in YourModule's module.config.php create routes pointing to your own UserController.

Edit Add your custom UserController to your ZfcUserOverride module. Then add it to this same module's module.config.php to the service manager config and alias it with zfcuser which is the name ZfcUser uses.

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

Edit 2: Also don't forget to add this to your module.config.php:

'view_manager' => array(
    'template_path_stack' => array(
        'zfcuser' => __DIR__ . '/../view',
    ),
),

This is required if you're overriding ZfcUser's view scripts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top