8

My block file path is

app\code\local\Company\Modulename\Block\Product\View\View.php

In this file method name is getId()

Now how to call call this method in controller?

2 Answers 2

13

Hi you can call this block by using below code at controller.

$block=$this->getLayout()->createBlock('blockPrefix/product_view_view');

$Id=$block->getId();

Block Prefix is define at app\code\local\Company\Modulename\Block\Product\View\View.php under tag

   <blocks>
        <blockPrefix><!-- guess that modulePrefix is your module block prefix -->
        <class>Company_Modulenam_Block</class>
        </blockPrefix>
    </blocks>

See at how to call a block check at here

12

If your block is already attached to the action of the controller you can call it like

$this->getBlock('block.name.here')->getId()

If the block has nothing to do with your current action you can call it like :

Mage::getBlockSingleton('[module]/product_view')->getId()
1
  • 2
    I'm not sure the second example works in, at least not in 1.9.2.1. The method's doc block says it's deprecated and to use self::helper() which is odd in itself, because blocks and helpers are different things!
    – scrowler
    Mar 24, 2016 at 1:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.