# Use different layout for each module

**URL:** <https://discourse.laminas.dev/t/use-different-layout-for-each-module/3300>\
**Category:** Components & MVC\
**Tags:** laminas-eventmanager, laminas-mvc, laminas-view\
**Created:** [April 18, 2023, 10:40pm UTC](https://discourse.laminas.dev/t/use-different-layout-for-each-module/3300 "2023-04-18T22:40:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Super0Yt](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/super0yt/32/1430_2.png) [@Super0Yt](https://discourse.laminas.dev/u/Super0Yt)\
**Post date:** [April 18, 2023, 10:40pm UTC](https://discourse.laminas.dev/t/use-different-layout-for-each-module/3300/1 "2023-04-18T22:40:16Z")

</div>

**Is it possible to set up a layout for each module?**

I am following an Oleg Krivtsov’s tutorial, but I don’t understand how to create a layout for each module. I’m struggling with the use of partial, helper, and listener to accomplish this.

_ **Can you recommend any resources that provide a simple solution to my question to understand how this thing works** _

Thank you.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [April 19, 2023, 6:50am UTC](https://discourse.laminas.dev/t/use-different-layout-for-each-module/3300/2 "2023-04-19T06:50:12Z")

</div>

> [@Super0Yt](#):
>
> I’m struggling with the use of partial, helper, and listener to accomplish this.

With a listener you are on the right track, because for each request it must be checked which is the target module for the current request. The configuration of all registered modules is always loaded and even cached when the application is started, therefore it needs a listener who listens for [an event in the application process](https://docs.laminas.dev/laminas-mvc/mvc-event/).  
But since a layout is only needed when an output is actually rendered from the view layer, it can be started very late in the process.

The documentation of laminas-eventmanager contains a tutorial how to create a listener in a laminas-mvc based application and also the registration of the listener:

> **[Usage in a laminas-mvc Application - laminas-eventmanager - Laminas Docs](https://docs.laminas.dev/laminas-eventmanager/application-integration/usage-in-a-laminas-mvc-application/)**
>
> Implement events, signal slots, aspects, and observers!

The documentation of laminas-view contains a cookbook recipe for your use case:

[https://docs.laminas.dev/laminas-view/cookbook/setting-module-specific-layouts/](https://docs.laminas.dev/laminas-view/cookbook/setting-module-specific-layouts/)

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [April 19, 2023, 6:53am UTC](https://discourse.laminas.dev/t/use-different-layout-for-each-module/3300/3 "2023-04-19T06:53:44Z")

</div>

Some more tutorials which describe the integration of components into applications (e.g. cache, form, session) can be found in the tutorials section:

> **[tutorials - Laminas Docs](https://docs.laminas.dev/tutorials/#application-integrations)**
>
> Learn how to create laminas-mvc applications, get in-depth guides into components, and discover how to migrate your applications to version 3!

---

<div class="post-metadata">

**Author:** ![visto9259](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/visto9259/32/669_2.png) [@visto9259](https://discourse.laminas.dev/u/visto9259)\
**Post date:** [April 21, 2023, 1:06pm UTC](https://discourse.laminas.dev/t/use-different-layout-for-each-module/3300/4 "2023-04-21T13:06:21Z")

</div>

@Super0Yt ,

There is one simple way to change the layout for a given module which is to set the desired layout for the view that will be returned by the controller. If your controller is an extension of the `AbstractController`, which is the case most of the time in an MVC app, then you can use the `layout()` controller plugin:

```auto
$view = new ViewModel([...your variables...]);
$this->layout('mylayout');
return $view;

```

Where `'mylayout'` is defined in your view stack.

This is useful if you need to change the layout for special cases where your default layout is not appropriate.

Hope this helps

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [April 21, 2023, 1:28pm UTC](https://discourse.laminas.dev/t/use-different-layout-for-each-module/3300/5 "2023-04-21T13:28:03Z")

</div>

> [@visto9259](#):
>
> …then you can use the `layout()` controller plugin…

Or the view helper:

[https://docs.laminas.dev/laminas-view/helpers/layout/](https://docs.laminas.dev/laminas-view/helpers/layout/)

But the requirement in the original post, seems to be different:

> [@Super0Yt](#):
>
> …how to create a layout for each module.

---

<div class="post-metadata">

**Author:** ![visto9259](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/visto9259/32/669_2.png) [@visto9259](https://discourse.laminas.dev/u/visto9259)\
**Post date:** [April 21, 2023, 1:33pm UTC](https://discourse.laminas.dev/t/use-different-layout-for-each-module/3300/6 "2023-04-21T13:33:26Z")

</div>

I agree with you. I was just providing a quick solution just in case he was looking to do this for one module.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [April 21, 2023, 1:37pm UTC](https://discourse.laminas.dev/t/use-different-layout-for-each-module/3300/7 "2023-04-21T13:37:01Z")

</div>

Don’t worry, I didn’t want to contradict you. Let’s wait and see if there is any feedback on what is really needed in the end. 😃

---

<div class="post-metadata">

**Author:** ![Super0Yt](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/super0yt/32/1430_2.png) [@Super0Yt](https://discourse.laminas.dev/u/Super0Yt)\
**Post date:** [April 21, 2023, 1:53pm UTC](https://discourse.laminas.dev/t/use-different-layout-for-each-module/3300/8 "2023-04-21T13:53:01Z")

</div>

but in this case i should use `$this->layout` for all my controllers.  
what i ask for is how to define a unique layout for each module, like @froschdesign proposethe the solution it’s by using listener and some more code that i can’t understand now and in that is the solution.  
@visto9259 thank you for your solution

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [April 21, 2023, 2:04pm UTC](https://discourse.laminas.dev/t/use-different-layout-for-each-module/3300/9 "2023-04-21T14:04:54Z")

</div>

If you need help, just ask here.

---

<div class="post-metadata">

**Author:** ![visto9259](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/visto9259/32/669_2.png) [@visto9259](https://discourse.laminas.dev/u/visto9259)\
**Post date:** [April 21, 2023, 3:32pm UTC](https://discourse.laminas.dev/t/use-different-layout-for-each-module/3300/10 "2023-04-21T15:32:32Z")

</div>

@Super0Yt ,

Just some guidance if you want to implement this using a listener.

You need to attach a listener to the `dispatch` event that is triggered by the `AbstractController`.

You can attach the listener to the shared event manager of the application and I suggest a priority -101 so that it is called pretty much as the last thing before rendering starts (i.e. the default layout is set, the view models have been injected, no errors were found, etc.)

Here’s a simple way of doing it:

In a `Module` class somewhere:

```auto
class Module 
{
    public function onBootstrap(MvcEvent $event)
    {
        $application = $event->getApplication();
        $sharedEvents = $application->getEventManager()->getSharedManager();
        $sharedEvents->attach(AbstractController::class, MvcEvent::EVENT_DISPATCH, [$this, 'setLayout'], -101);
    }

    public function setLayout(MvcEvent $event)
    {
        $controller = $event->getTarget();
        // use $controller to find the layout template that you need for the controller
        $layout = somelogic($controller);
        // use the layout() plugin to set the layout template
       $controller->layout($layout);
    }
}

```

This is very basic but it should give you the gist of it.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [April 21, 2023, 3:54pm UTC](https://discourse.laminas.dev/t/use-different-layout-for-each-module/3300/11 "2023-04-21T15:54:18Z")

</div>

Not necessary, everything can be taken from the documentation. See the link above: “Setting module-specific Layouts”

And if you check the usage in the documentation of laminas-eventmanager, then you will see that the module class can stay nice and clean. 😃
