Blocks
Looking within the helloWorldAction()
method, you can see a call towards the createBlock()
method with the string 'core/text'
as a parameter. Where does 'core/text'
come from and what does it mean? In order to understand that, we will further extend our config.xml
file by adding a blocks
element to it as follows:
<?xml version="1.0"?> <config> <!-- … other elements ... --> <global> <blocks> <foggyline_happyhour> <class>Foggyline_HappyHour_Block</class> </foggyline_happyhour> </blocks> </global> <!-- … other elements ... --> </config>
The element foggyline_happyhour
is known as the class group. The element foggyline_happyhour
is a class group name and its inner class element is basically a shortcut for your extensions Block
type PHP classes. For example, the following is a modified code for our helloWorldAction()
method shown previously:
<?php class Foggyline_HappyHour_HelloController extends Mage_Core_Controller_Front_Action { public function helloWorldAction() { $this->loadLayout(); $block = $this->getLayout()->createBlock('foggyline_happyhour/hello'); $block->setText('Hello World #2.'); $this->getLayout()->getBlock('content')->insert($block); $this->renderLayout(); } }
You can see that we are no longer calling the createBlock()
method with 'core/text'
but with the 'foggyline_happyhour/hello'
parameter. This is like telling Magento to load the hello class (the Hello.php
file) that can be found under the classpath mapped by the foggyline_happyhour
class group. As the foggyline_happyhour
class group has its class value set to Foggyline_HappyHour_Block
, Magento expects to find the app/code/community/Foggyline/HappyHour/Block/Hello.php
file.
How and why exactly does Magento expect the Hello.php
file to be at a certain location? The answer to this lies in a robust autoloading functionality of the Magento system based on a configuration and file naming convention. You can split all Magento classes into four parts that we'll call the vendor namespace, extension name, class group, and filename itself.
The vendor namespace helps us prevent name collisions between extensions, letting you know which extension is the owner of the class. For example, all core Magento extensions use the mage
namespace.
The module name plays a crucial part in the autoloading system. All the proper customization of Magento is done through individual extensions.
The class group is a sort of alias defined within the extension's configuration file, an alias towards a class folder within the extension directory. There are several main types of class groups, such as the one for Model
, Block
, Helper
.
Finally, the name of the file itself. Each class should have a unique name within a class group that describes its intended use or function.
Magento's autoloading functionality uses these parts to determine where to find the source for a given class as shown in the following example: VendorNamespace/ModuleName/ClassGroup/FileName.php
.
Go ahead and create the file Hello.php
with the following content:
<?php class Foggyline_HappyHour_Block_Hello extends Mage_Core_Block_Text { }
Now that you have modified helloWorldAction()
and created the Hello.php
class file, go ahead and open the http://magento.loc/index.php/happyhour/hello/helloWorld
URL in the browser. The result should be the same as in the previous case; you should be able to see the fully loaded Magento page with the Hello World #2. message shown under the content area.
Our Hello
block class extends Mage_Core_Block_Text
. However, chances are that most of the time you will be extending the Mage_Core_Block_Template
class, where your Hello
block class might look like something as follows:
<?php class Foggyline_HappyHour_Block_Hello extends Mage_Core_Block_Template { public function __construct() { parent::__construct(); $this->setTemplate('foggyline_happyhour/hello.phtml'); } }
The difference between extending Mage_Core_Block_Text
or Mage_Core_Block_Template
is that the latter requires you to define a view *.phtml
file under the theme
folder, and thus, is more designer friendly. In order for it to successfully work, you need to create the app/design/frontend/default/default/template/foggyline_happyhour/hello.phtml
or app/design/frontend/base/default/template/foggyline_happyhour/hello.phtml
file. You might find the latter to be a safer location for your view files, as it is not dependent on your customer theme and package settings. Thus, the Magento theme fallback mechanism will always pick it up. Now if you put your Hello World #3.
string within the hello.phtml
file and then re-open the http://magento.loc/index.php/happyhour/hello/helloWorld
URI in your browser, you should again see the fully loaded Magento page with the Hello World #3. message shown under the content area. Our goal here is to give you the basics of functional extension, so we will now leave the controllers and blocks behind and move to the model.
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.