Programmatically: How to create custom Magento helpers
Step 1: Create a new PHP file
Create a new PHP file in the app/code/local/ YourCompany/YourModule/Helper directory (replace YourCompany and YourModule with your actual company and module names). Let's call it MyHelper.php. MyHelper.php
namespace YourCompany\YourModule\Helper;
class MyHelper extends Mage_Core_Helper_Abstract
{
public function getHelloWorld()
{
return 'Hello, World!';
}
}
Step 2: Declare the helper in the module's config file
In your module's etc/module.xml file, declare the helper by adding the following code:
YourCompany_YourModule_Helper_MyHelper
Step 3: Use the helper in your Magento code
Now you can use your custom helper in your Magento code by injecting it into your class. For example, let's say you have a controller class MyController.php in the same directory:
namespace YourCompany\YourModule\Controller;
class MyController extends Mage_Core_Controller_Varien_Action
{
protected $_helper;
public function __construct(YourCompany_YourModule_Helper_MyHelper $helper)
{
$this->_helper = $helper;
}
public function indexAction()
{
$message = $this->_helper->getHelloWorld();
// Use the message in your controller action
}
}