What's the best way to load the Mage.php (Magento) class within Symfony2. I've read documentation and and tried different autoloader and classloader configurations but the only thing works for me is below.. Ideally, I'd like to use "Best Practices" which my current solution doesn't do. This would probably involve not using the require_once and instead loading from autoload.php or using composer.json

 * var/www/symfony/app/console -- THIS WORKS ONLY IN THE SYMFONY CONSOLE
    if($env == 'prod'){
    require_once '/var/www/production/app/Mage.php';
    require_once '/var/www/development/app/Mage.php';

This code allows me to do Magento tasks ($product->save(), backoffice type things, batch updating products) from within the Symfony Environment without breaking down the Core Magento functionality and putting the data at risk.

For anyone curious why I would want to do this

The Symfony Framework is easier for rapid development of back-end processes and back-end reporting. Twig and the Symfony Form functionality works well for reporting.


Not really an answer to your question but in my honest opinion mixing 2 frameworks (or whatever you want to call Magento) is never a good solution.

Use the Magento API's to do the Magento related tasks calling them from your Symfony application. This way they can run separate and won't mess with eachother.

  • Sander, I think you're suggestion to use the Magento API's is "Best Practice" option. Thank you for mentioning that. – JustinP Apr 10 '14 at 13:17
  • There's an entire page on this issue here and a Symfony LiipMagentoBundle – JustinP Apr 10 '14 at 13:41

As Magento is very special, there is no such thing as best practice yet, but there are a few possible ways.

What you described is the one way, besides it needs to respect some more things. 1. load your framework (means composers autoload file) 2. require magentos Mage.php 3. call Mage::App($mageRunCode, $mageRunType) with the parameters you would use in the index.php

You need to test your code well, as every class tried to load can crash your app, as magento throws an exception for not found classes. This also affects the class_exists() function.

Then there is a second way, to use composers autoloader for magento. in basic this means:

"autoload":{ "classmap": [ "../path/to/magento/app/code", "../path/to/magento/lib" ] },

but to remove the magento own autoloader can get a bit hacky then.

For me, I use the first approach in a silex project and its very stable. But you need to have an eye on everything which makes use of class_exists.

  • 1
    +1 for As Magento is very special, there is no such thing as best practice yet, but there are a few possible ways. – ash Apr 27 '14 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.