juliangorge
juliangorge

Reputation: 36

Extending module but keep no-overwrite template map

I was looking for how to re-use my User module across other projects. As I have custom features for each my clients, I decided to create:

  1. One generic UserModule to be packaging
  2. One extend-module called UserCustom

UserModule has the following views: login and add (for register). UserCustom is extending Module.php and no more.

<?php
namespace UserCustom;

use Laminas\Mvc\MvcEvent;
use Users\Module as UserModule;

class Module extends UserModule
{

    public function getConfig()
    {
        return include __DIR__ . '/../config/module.config.php';
    }

Now, imagine that I want to change ONLY login view to add a logo.

I do not understand how can I overwrite specific view files and keep the others. With my current config, I must to overwrite every view that UserModule has (overwrite to new file location or even vendor).

I've declared the following code to login new view file: UserCustom module.config.php:

'view_manager' => [
    'template_map' => [
        'user/login' => __DIR__ . '/../view/user-custom/new_login.phtml',

But, when I go to /add page, its seems like I need to declare every view. Laminas\View\Renderer\PhpRenderer::render: Unable to render template "user/add"; resolver could not resolve to a file

My UserModule has a

'template_path_stack' => [
 'user' => __DIR__ . '/../view',
],

but I do not know if its helps me.

Thank you

Upvotes: 0

Views: 162

Answers (1)

ALTAMASH
ALTAMASH

Reputation: 26

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.

Upvotes: 1

Related Questions