The Magento Registry

piaoling  2011-05-19 10:01:04

The Magento registry provides an easy way for objects to share information, without actually have to know about each other. In short, it is basically an array that can be accessed anywhere in Magento because it is a static function. If you’re not sure what a static function is, let me explain. Non-static functions, or dynamic functions cannot be called on an object that hasn’t been instantiated as they need an instance of the class to function. Static functions differ in that these can be called with the object being instantiated. To call a static function we use the format ClassName::StaticFunctionName().

Registering Data With The Magento Registry

To register data with the registry we use the following static function:

?
1
2
3
4
// Mage::register($key, $value);
$productId = 1; // Any valid product ID
$_product = Mage::getModel('catalog/product')->load($productId);
Mage::register('product', $_product);

First, we create a product model and store it in the $_product variable. This is just a common example and we could store any type of data in there. Once we the product model, we call the static method register on the Mage class. The first parameter to this function is the key by which the data will be stored. When you come to retrieve the object from the registry, it is this key that will allow you to get it. The second parameter is the data itself.

The above code was just an example but hopefully illustrates how to add data to the registry.

Retrieving Data From The Magento Registry

Retrieving data is even easier than adding data.

?
1
2
3
// $value = Mage::registry($key)
 
$_product = Mage::registry('product')

To access the product we saved earlier using the product key, we simply pass the key to the static function registry. The beauty of this is that we can do this in different files. For example, a controller could assign data to the registry which you could then access using your custom modules! You can also set your own registry information, which again is useful when creating custom modules.

$this->getProduct()

You may have noticed that in a lot of product templates you have $_product = $this->getProduct(). This function returns a product model for the current page from the database. I originally thought that this function must perform some database queries and extract the product itself, however, after looking in the Block class code files, I realised that it just extracts a value from the registry stored in the product key (exactly like our example above!).

Get The Current Category Model

If you’re on a page that has an associated category, it is quite easy to access that category model any where in Magento. For example, the category page obviously has an associated category model, so by calling the following code, you will get a copy of that model.

?
1
2
$_category = Mage::registry('current_category');
echo $_category->getName();

As well as the category page, product pages also have a category an the above code can be used there.

Get The Current CMS Page Model

If the current page you’re on is a CMS page (homepage, 404 page or any custom CMS page) you can use the following code to access the CMS Page Model

?
1
2
3
$_page = Mage::registry('cms_page');
echo $_page->getTitle().'<br/>';
echo $_page->getContent();

Conclusion

Hopefully this brief primer on the Magento registry illustrates it’s potential and highlights how you can quickly and easily share information via classes without having to define class relationships.

类别 :  magento(258)  |  浏览(3345)  |  评论(0)
发表评论(评论将通过邮件发给作者):

Email: