Thursday 26 June 2014

On 12:06 by Unknown in , ,    No comments
The main reason, in fact the inspiration behind this blog is this THREAD. In this thread, the user want to add layout that render by controller B by using controller A along with its layouts. For those who didn't understand the thread, I will explain the situation little bit.
So I have a module Programmerrkt_Checkout, which is used to provide two checkout views for my site. So in my module there are two controllers. Let us call them as Controller A and Controller B. Whenever url request these two controllers, it will show its own layouts in frontend. Let us create our module. Our config file should look like this

config.xml

Location:app/code/local/Programmerrkt/Checkout/etc/config.xml

As you can see, our config file do mainly 2 things here
  • Controller Rewrite
  • Layout updation
Controller Rewrite :-
It rewrites Mage_Checkout's CartController. This is because Our module is intended to provide two views for checkout/cart
Layout Updation :-
We defined our layout xml file here. It is using to hold the layouts of our controllers.

programmerrkt_checkout.xml

Location:app/code/design/frontend/base/default/layout/programmerrkt_checkout.xml

Here checkout_a_index is the layout handler for controller A. Similarly checkout_b_index is the layout handler for controller B. When www.yoursite.com/index.php/checkout/a loads it will loads content inside in checkout_a_index handler. When www.yoursite.com/index.php/checkout/b loads, it will load content that reside under checkout_b_index handler.
Now how these handlers are loading for A and B view. This is achieved through controllers. Let us look code inside our controllers

Controller B

Location:app/code/local/Programmerrkt/Checkout/controllers/BController.php

This is our controller B. As you can see it call load and render layouts. This will render content comes inside in checkout_b_index handler in its content section. (There are other layout handlers that are loading while do this. An example is default handler).

Controller A

Location:app/code/local/Programmerrkt/Checkout/controllers/AController.php
As I already stated it will render blocks that comes under checkout_a_index handler.

View

From the layout files, you can see that we are setting two templates to set view for A and B. Let me show the content in this file

a.phtml

Location:app/design/frontend/base/default/template/programmerrkt/checkout/a.phtml

b.phtml

Location:app/design/frontend/base/default/template/programmerrkt/checkout/b.phtml
Now load the url www.yourdomain.com/index.php/checkout/a, you will see following output

I am Controller A

I am content of A controller and I lies is layout definition for A controller
Similarly load the url www.yourdomain.com/index.php/checkout/b, you will see following output

I am Controller B

I am content of B controller and I lies is layout definition for B controller

The Real Problem

Now everything is perfect. Here the problem begins. Now we want to load the layout content of controller B, when url request for controller A. ie when www.yourdomain.com/index.php/checkout/a is requested, it should provide following output

I am Controller A

I am content of A controller and I lies is layout definition for A controller

I am Controller B

I am content of B controller and I lies is layout definition for B controller
How can we accomplish that ? Well, in my mind there are two awesome methods that we can depend on. They are
  • Use an event Observer to add layout handler of controller B to controller A
  • Make changes to codes in Controller A in such a way that it will load layout of controller B also
Using Controller
We can achieve what we want here by changing our controller A code like this

Controller A

Location:app/code/local/Programmerrkt/Checkout/controllers/AController.php
Here what we are done is `manual coding` and replacing of loadLayout() . Why we are doing loading of layout manually? The answer is, if we add $update->addHandle('checkout_b_index') before or after of loadLayout(), it does not make any effect on output. The reason is well explained by alanstorm in this THREAD.
Take a look on loadLayout() definition.

Action.php

Location:app/code/core/Mage/Core/controller/Varien/Action.php
This code reveals the fact that, in your controller we are actually using the same method that is used by default loadLayout() method,except for the addition of of a custom handle before calling loadLayoutUpdates(). (!important).
That's it. We achieved the desired result through controller. Now let us move on to our second approach. That is observer method
Event Observation
This is the most understandable method. Here we are observing for an event `controller_action_layout_load_before`. Add this code to config.xml

Config.xml

Location:app/code/local/Programmerrkt/Checkout/etc/config.xml
Add this code to observer

Observer.php

Location:app/code/local/Programmerrkt/Checkout/Model/Observer.php
when this event is listening, according to my understanding, magento resides in between `$this->addActionLayoutHandles()` and `$this->loadLayoutUpdates()`. That means it already loaded default, store, theme and action handles. So we are in the perfect position. We can add layout handle of controller B, if the current action is `checkout_a_index` (I used `if` condition for check that.). Note: This is a good tutorial that depicts this observer calling LINK
Now go and load url www.yourdomain.com/index.php/checkout/a. You can see the desired output in your page. :)

0 comments:

Post a Comment