Thursday, 26 June 2014
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.
As you can see, our config file do mainly 2 things here
Take a look on loadLayout() definition.
That's it. We achieved the desired result through controller. Now let us move on to our second approach. That is observer method
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).
As I already stated it will render blocks that comes under checkout_a_index handler.Controller A
Location:app/code/local/Programmerrkt/Checkout/controllers/AController.php
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
Now load the url www.yourdomain.com/index.php/checkout/a, you will see following outputb.phtml
Location:app/design/frontend/base/default/template/programmerrkt/checkout/b.phtml
Similarly load the url www.yourdomain.com/index.php/checkout/b, you will see following outputI 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
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 outputHow can we accomplish that ? Well, in my mind there are two awesome methods that we can depend on. They areI 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
- 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 thisHere 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.Controller A
Location:app/code/local/Programmerrkt/Checkout/controllers/AController.php
Take a look on loadLayout() definition.
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).Action.php
Location:app/code/core/Mage/Core/controller/Varien/Action.php
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.xmlConfig.xml
Location:app/code/local/Programmerrkt/Checkout/etc/config.xml
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 LINKObserver.php
Location:app/code/local/Programmerrkt/Checkout/Model/Observer.php
Now go and load url www.yourdomain.com/index.php/checkout/a. You can see the desired output in your page. :)
Subscribe to:
Post Comments (Atom)
Stack Exchange
Recently Answered
Popular Posts
-
In this post, I would like to show you how can we get product option values. For this, let us start with creating a sample product that h...
-
Static blocks are very useful tools in Magento. We can effectively use them in different situations. They provide us enough power on content...
-
Some times, it would be great if we can add a collapse/expand functionality in CMS Pages in Magento. A perfect example would be a read more ...
-
By default every Magento product list page has to modes of view. They are List and Grid . We can set default mode through admin. For this ...
-
Here in this tutorial, I would like to share you how can we set page size for a particular category. The Page Size is actually a property...
-
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...
-
Introduction ---------------------- When I started to do Module Development in Magento, a lot of time I got an error like this Fat...
Blog Archive
Categories
Powered by Blogger.
0 comments:
Post a Comment