2

I am using
$smallUrl = $this->helper('catalog/image')->init($_newProduct,'image')->resize(200);

this code to dynamically fetch images using ajax at root directory of Magento. but I think i cant use $this directly on root directory.

i added

require_once '../app/Mage.php';
Mage::app();

but it doesn't work. what is a workaround here?

2 Answers 2

4

Use

Mage::helper('catalog/image')

instead of

$this->helper('catalog/image')

Additional points

In order to work this, you need to make sure app/Mage.php is included in your custom php file. You have specified your php file stays in root directory folder. In that case you need to include these codes first in your custom php file.

/**
 * Error reporting
 */
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);

require_once 'app/Mage.php';

Mage.php is a final class. Hence it cannot extend. This file holds lot of useful common methods. Such a method is helper.

We can use Mage::helper everywhere in Magento, since helper classes are globally available all over Magento.

Generally helper classes are accessed like this

Mage::helper('helper_alias/classname');

Here helper_alias is used to find which module's helper should use. In our example, it is catalog. This means Magento should use Mage_Catalog_Helper directory for find helper class. classname stands of which helper class should use. If it is not sepcified, by default magento use Data class. In example, classname is specified as image. This means Magento will finally use class Mage_Catalog_Helper_Image class for it.

All helper classes are globally available. This means we can use them almost everywhere in magento. This is because, Magento will "register" helper classes using the method Mage::register. This particular method is use to make something globally available. When we think about this, it is highly logical, since a "help" should be available everywherer :-) So if we make helpers only available to the defined module, then that is not good. The point is all helpers of every module should be globally accessible so that we can use any one of module's help at any time.

Since helpers are globally available, they add lot of advantages and similarly disadvantages. Always try to put useful common methods in helper.

2

$thismean it is an object of a class and as you have using outside magento it not a given any class. If you want to call any helper function at outside magento you need to use

Mage::helper('helpreficxx/yourhelperclass')

So use Mage::helper('catalog/image') instead of $this->helper('catalog/image').

2
  • 1
    are you sure about the very first point. Can we use $this inside a block ? I think we can use $this only when we are inside helper class ! Commented Oct 13, 2014 at 6:28
  • not confirm. i will check...
    – Amit Bera
    Commented Oct 13, 2014 at 6:29

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.