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.
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.
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.
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.
'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.
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.
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.