2

We are slowly migrating our current system to Symfony2, but the majority of the codebase is still in an inhouse framework. I want to leverage some functionality built in Symfony2 from within classes in the old legacy framework. Is there an easy way to access a Symfony2 service from outside of the Symfony framework?

class MyOldClass extends SomethingOld
{
    public function getSomethingViaSymfony()
    {
        $service = new SomeSymfonyService();
        $results = $service->getResults();
    }
}

My assumption is that this would be a failure, for the dependencies wouldn't be injected.

1 Answer 1

1

you would need to initialize symfony without dispatching any actions. this basicly means taking the code from the symfony front controller file in web/index.php, modifying it a bit and paste it to some initialization file of your legacy app.

// legacy/init.php
require_once 'PATH_TO_SYMFONY/app/bootstrap.php.cache';
require_once 'PATH_TO_SYMFONY/app/AppKernel.php';


$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();

$GLOBALS['service_container'] = $kernel->getContainer();

note that this code is not tested but i'm pretty sure it'll work because symfony is great;)

also you could think about a different strategy in which you embed the legacy action into symfony and not the other way around.

you would have to implement a legacy controller and give it a catch all route at the end of the routing definition. this controller can initialize the legacy application and dispatch actions to it. afterwards you can successively define new routes at the top of the routing file and dispatch them with symfony.

this strategy is imho much better because you can leave the legacy application almost untouched and kill it piece by piece.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.