Getting started with MageBridge - Yireo

Fetch your own Magento data from MageBridge

MageBridge allows you to integrate Magento into Joomla!, and for this to work, data are transferred in various ways from Magento to Joomla! using the MageBridge API. This API could also be used to fetch data for your own Joomla! extension. This guide helps you with the right steps.

Dealing with bridge requests

The architecture used by MageBridge is a bit complicated to safeguard performance, but using it in your own code is a lot easier. Because Magento is known to be slow, initializing Magento through MageBridge should never be done more than once on a single page. Instead of making muliple calls to Magento, MageBridge there for tries to bundle all needed calls into one single HTTP request, so that Magento is only started once.

To make this possible, there are two vital parts within the MageBridge architecture: The bridge itself, but also the register. The register is used to collect all needed bridge-calls, before the bridge actually makes these calls. The bridge-response is than transferred back to those Joomla! extensions that made the initial registration.

Bundling API-calls

Looking at a single Joomla! page, that page is either served by a MageBridge-driven component, or it is served by another component that might be parsed by Joomla! plugins. In both cases, the Joomla! modules can be seen as optional, while the core bridge-functionality is made available through either component or plugin. In general, we could say that the bridge is executed when either the Joomla! component is loaded, or when a Joomla! plugin needs it.

Typically Joomla! modules that need data from Magento do not build the bridge themselves. They are there for pre-loaded (meaning that their API-call is registered in the MageBridge register very early in the Joomla! bootstrap), which is discussed in a different tutorial.

Initializing the bridge-objects

When using MageBridge, you can call two main MageBridge classes that are referred to as the bridge-object and register-object, or just more simply the bridge and the register:

$bridge = MageBridgeModelBridge::getInstance();
$register = MageBridgeModelRegister::getInstance();

If the MageBridge System Plugin is active (which it should), the MageBridge autoloader is enabled, which means that you do not need to include any of the MageBridge class-files: The right path is included automatically.

Building the bridge and fetching the result

Most commonly you will either make an API-call to the regular Magento API, or fetch a Magento block. The Magento API is normally available through SOAP and XML-RPC, but in the case of MageBridge, multiple calls to the API are simply wrapped into the MageBridge API (which is actually based on JSON, and not SOAP or XML-RPC).

The logic is to first register the call, then build the bridge and than fetch the result from the bridge. The register-object is used here with its method add() to add a new API-request. The second argument here refers to the resource-name in the Magento API. While MageBridge offers some extra APIs, you can also use the regular Magento APIs here. The third argument is an array of arguments to be passed through to the API-method.

$id = $register->add('api', 'magebridge_user.save', $user);

After this, the bridge can be build. The MageBridgeModelRegister::add() method returned an unique identifier that we can use to quickly scan the bridge-data to find the right result.

$bridge->build();
$segment = $register->getById($id);

The register-object contains a simple PHP-array which is constructed in Joomla!, sent through the bridge to Magento where various attributes might be added, and than the array is sent back to Joomla! and pushed back into the register-object. The MageBridgeModelRegister::getById() method returns a segment of this PHP-array, which again contains data. If you want to fetch the data directly, you can also use the method MageBridgeModelRegister::getDataById().

Fetching a block

Using the same objects as above, we can also fetch a Magento block from the API. In this case, the second argument is referring to the block name defined in the Magento XML layout-files. The third argument is not needed.

$id = $register->add('block', 'my-block-name');
$bridge->build();
$block = $register->getDataById($id);

Instead of fetching the block from the register-object, you can also use the MageBridgeModelBridge::getBlock() method for this. Whatever you like.

$block = $bridge->getBlock('my-block-name');

While you can style a Magento block within the Joomla! template-CSS, if you need Magento JavaScript for that block, you need to register it as a separate request:

$register->add('headers');

There is no need to fetch the result: The MageBridge System Plugin will automatically parse the Magento headers and integrate them into the Joomla! headers.

Created on Saturday, 24 July 2010
Modified on Saturday, 24 July 2010

About Yireo

Yireo tries to help webdevelopers build successful Joomla! and Magento sites.

More about Yireo