Blog Posts

Integrating Magento into Symfony2

So last week four developers sat together on a regular Hackday to see what’s needed to hook up Magento into Symfony. To make this short the outcome is a Magento bundle for Symfony2.

When we met in the morning we weren’t even sure what exactly to try out but soon agreed on implementing a Symfony authentication which uses the Magento customer database.

Autoloader and Login

Starting our hacking, the first problem appeared quickly with incompatible class loaders: Unfortunately, the Magento class loader isn’t designed for missing class files, so every time Symfony tries to check for existence of a class which doesn’t exist there will be a fatal error. As we knew this problem from other projects a patch was already at hand.

Everything went quite smoothly then as integrating Magento into another project is basically just including the file app/Mage.php and then calling Mage::app(). Done.

Implementing the authentication itself seemed easy too as there is this simple method call in Magento which we added to our bundle within minutes to try out the login:

Mage::getSingleton('customer/session')->login($username, $password);

Session issues

But that’s where things got ugly: The sessions didn’t sync between Magento and Symfony. Integrating Magento without synced sessions doesn’t really make any sense as the user will loose his cart as soon as he browses from Magento to Symfony.

After a few hours of debugging and digging into both session handlers of Magento and Symfony the problem was found inside the __destruct methods. So here’s what happens:

  1. Symfony stores everything you save in the session inside Symfony\Component\HttpFoundation\Session.

  2. During destruction phase inside Magento Mage_Core_Model_Resource_Session::__destruct() calls session_write_close() and saves the session content to the database (depending on your Magento configuration).

  3. Next Symfony\Component\HttpFoundation\Session::__destruct() writes its own session information with the key _symfony2 into the session storage - too late!

The solution was to add a kernel.response event listener and call Symfony\Component\HttpFoundation\Session::save() so the Symfony session content gets into the session storage before Magento saves it to the database.

Promising results

After solving the ugly session problem the only thing left was implementing an security provider for Symfony which authenticates the user against the Magento customer database. The result is a proof-of-concept Symfony Bundle which already provides some useful features to Symfony:

  • Login for Magento customers.
  • Synchronized session to access the shopping cart.
  • Full access to Magento products inside Symfony.

Integrating Magento into Symfony has been proven certainly possible and we had fun learning more about the session handlers of both Magento and Symfony.

At the moment there’s no specific project at Liip planning to use this bundle, so contribution is welcome on GitHub:

Related Entries:
- Discussions and Pizza at PHPDay Italy
- RESTing with Symfony2
- How to manage patching a github hosted repo
- LaraconEU 2014 - Empowering the PHP people
- Of HHVM, Hack and the future of PHP

About the author

Comments [8]

Thomas, 21.09.2011 21:38 CET

Very interesting blogpost! Thanks for the experience sharing!

Cameron Manderson, 22.09.2011 06:36 CET

Very cool! :-) Good luck with the bundle! sure to give it a spin in the future!

Rick G, 12.10.2011 19:56 CET

Great work guys! That was way cool!

Magento developers, 31.12.2011 06:09 CET

It's great article. Thanks for sharing such great helpful information. Keep posting.

Magento Commerce, 16.03.2012 21:01 CET

How to import Magento Products Without moving the program? I dont want to transfer the old website to the new server.

Magento, 03.05.2012 18:53 CET

Is there any way that magento development can allow export of orders/transactions to Quickbooks, preferably without a new module? This could be a direct sync or a simple excel/csv export that I can then import to Quickbooks.

daniel, 05.12.2012 12:56 CET

At the moment there’s no specific project at Liip planning to use this bundle, so contribution is welcome on GitHub:

Is future development planed for this?

samuel sandeeep, 16.08.2013 12:47 CET

Hi Fabian,

Nice Article,thanks for sharing this information.

Magento Developers

Add a comment

Your email adress will never be published. Comment spam will be deleted!