What is the best way to extends MVC packages

Hello!
I am looking for the best way to build re-usable MVC packages for my projects.

For example: all my projects are using User module with generic controllers, entities, forms and views to registering users (username, password and email).
My problem occurs when one customer needs an extension of register form (adding a phone number).

As my generic User module is imported and located at vendor folder, how can I make to extend it behavior?

Can you name the used vendor package? It 's hard to say, because there are plenty of good and bad practices how to or how not to extend vendor packages. It would be easier to answer if we know the used package.

Approaches you should NOT use …

NEVER EVER CHANGE FILES IN VENDOR FOLDER! PERIOD!

Approaches you might use …

  • Write your own factories and define your own models and forms depending on your vendor package.
  • Override vendor config entries in your application config and use your own forms and models, that extend the vendor classes.

Hello!

Vendor package is “Juliangorge\Users”
And my new extend module is User.

I have been trying to set factory controllers and view_manager in new module.config.php
Regarding Entity and Form, It seem I need to use listener and events (following this interesting article: Use 3rd party modules in Zend Framework 2 · Jurian Sluiman)

Hi @juliangorge,
A long time ago I wrote this post at the time of Zf2. If it helps you get some idea to add fields in LaminasUserModule this post of mine might help you in some way. My blog is not HTTPS secured. So, feel free to check it.

Thanks!

1 Like

Even if the hint is correct, in this case nothing in the folder should be overwritten. An existing module should be extended, like adding or extending a validator of laminas-validator.

There are different ways to do this. For your register form can use the configuration. You can add the full specification to the module configuration, which allows overwriting or extending in the application.
Or you create a factory or delegator which adds new elements from the configuration.
What I often see: you extend the existing register form class and call the parent init method and add new elements. (In the configuration of your application you have to map the new form to the register form of your module.)

I would definitely keep it simple. For example, use the configuration to specify the form and you do not need custom factories, delegators, listeners, etc.

If you need a concrete code example, please ask!

1 Like

Froschdesign, thank you so much for your time and sorry for my late anwser.

I was playing with my extension module and I do not know how to overwrite specific views of vendor package in my extension module.config.php. May I need to overwrite all of this?

I wrote a question of this yesterday.

Hi @juliangorge,

To override any action view file you’ve to understand the basic functionality of how Laminas search a view file. To understand this you can view the configuration here and I’ll explain it below.

return [
  'template_stack' => [  
       // template stack means where will all the view files of every controller and its action will be found.
  ],
  'template_map' => [
     // the default map would be as follows
     // 'ModuleName/ControllerName/ActionName' => 'actual path of file.' 
    'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
  ],
];

To override an action template file you’ve defined it in your module.config.php file like below.

return [
  'template_map' => [
    'application/index/index' => __DIR__ . '/../view/mymodulename/mycontrollername/myactionname.phtml',
  ];

I hope I’ve answered your question.

1 Like

Hello @ALTAMASH80,

Let me see if I understand, do I need to define every template file in my reusable package and then override only what I want?

My reusable package only define this:


    'view_manager' => [
        'template_map' => [
            'users/layout' => __DIR__ . '/../view/layout/layout.phtml',
        ],
        'template_path_stack' => [
            'users' => __DIR__ . '/../view',
        ],
        'strategies' => [
            'ViewJsonStrategy',
        ],
    ],

I really appreciate your time and help.

PD: Maybe is a problem with namespace. Because Auth Controller (who extends from Juliangorge\Users) is looking for an “auth/user/__” file

Hi @juliangorge,

This template path stack override feature is also new information for me. Thanks for that. If this one line of code can change the all the view templates of a module. It is an awesome. Keep up the good work. As for the specific answer of a file can’t be found. You’ve to check yourself. If it is a opensource module dig into it.

1 Like

Hello @ALTAMASH80,
Regrettably, I could not to override successfully because Auth Controller use “auth/users” instead of “users”. Do you know how to “override” this auth/users?

Thank you so much!!!

Hi @juliangorge,

If you’re confident that the code which you’ve given above works.

Then you only need to add another line of code like the one below.

        'template_path_stack' => [
            'users' => __DIR__ . '/../view',
            'auth/users' => __DIR__. '/../view',
        ],

Thanks!

Hi @ALTAMASH80 , great to know about you!
Really thank you for all.

I added it (vendor folder) but it’s still doesn’t work. :confused:

Hi @juliangorge,

Changes in the vendor folder are not recommended. But as you’ve mentioned it is in the vendor folder. That means you can view its code. Look at the configuration of the module and do the changes accordingly. Look in the module/config/*.php files and look for the template path stack and template map configurations. Then try to play with the configuration by what I’ve shared above.

I hope it helps you. If not please accept my apology.