Viewing the Registry Contents in Magento

piaoling  2011-05-19 10:04:11

The Magento registry is a globally accessible store of data that is used by all aspects of the Magento system. Recently a user asked me how to see the contents of the registry. After a little play I found a way to do this, however, be warned: to accomplish this you need to temporarily modify a core Magento file (Mage.php). Read on for more information

Mage.php and the Mage class

The main file of Magento is a file called Mage.php and is stored inside the app folder. This class contains a lot of useful functions which you will probably notice. Most of these functions are static which means they can be called without instantiating the class. This is great for global data access as it allows you to access data stores and useful functions from anywhere in Magento.

Open up Mage.php and have a look through. You should notice the private variable called $_registry. This contains an array which stores all of the registry information. To be able to access this array directly, add the following line inside the Mage class.

?
1
public static function getRegistry() { return self::$_registry; }

This function will allow you to return the whole registry array as opposed to individual values. Using the following code, you will now have access to the registry array.

?
1
<?php $registryArray = Mage::getRegistry() ?>

Now that you have the array, you can go ahead and manipulate it like you would any other array, however, I wouldn’t recommend this. This array contains many large objects, which themselves contain large objects inside of them. If you tried to run the print_r() command on this array, it would probably eat up your systems memory and cause your browser to crash.

Viewing the contents of the Magento registry

Rather than running print_r() on the whole array, let’s just look at the key’s, their data types and values/class names.

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php $registry = Mage::getRegistry() ?>
<?php if (count($registry) > 0): ?>
  <h1>Registry Inspection: by Fishpig</h1>
  <ul>
    <?php foreach($registry as $index => $value): ?>
      <li>
        Registry['<a href="#"><?php echo $index ?></a>'] =
        (<em><?php echo gettype($value) ?></em>)
        <strong>
          <?php if (is_object($value)): ?>
            <?php echo get_class($value) ?>
          <?php elseif (is_array($value)): ?>
            Array
          <?php else: ?>
            <?php echo $value ?>
          <?php endif; ?>
        </strong>
      </li>
    <?php endforeach; ?>
  </ul>
<?php endif; ?>
<?php exit ?>

Run the above code and you should get a print out similar to the following:

Undo the Mage.php changes!

Now that you have seen the contents of the registry, make sure you remove the function that you added to Mage.php!

 

from:http://fishpig.co.uk/viewing-the-registry-contents-in-magento/

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

Email: