How to use Symfony 2 container in legacy application

I would like to integrate the legacy application with the Symfony 2 application - replacing more and more parts of the old application with Symfony components. The approach I would like to use is to use the Symfony 2 container in an inherited application that receives services that are already configured for the Symfony 2 application. The first services I would like to use are the session and the security context.

Questions:

  • Is it possible?
  • How to get a configured service container?

Additional information in an outdated application: typical PHP mess: separate PHP files as β€œcontrollers” (checking $ _GET and $ _POST for different execution paths). Each page includes init.php , which sets up startup, connecting to the database, etc. Session management has its own class (which I would like to replace), the data is extracted through calls to the static methods (!) Of the database objects.

+7
source share
2 answers

Using Symfony DIC as a stand-alone component is possible , but you will have to do a lot of things β€œmanually” (since you are not planning on using the full Symfony Framework from the beginning). You probably won't use DIC with all this legacy.

If you want to go this route, I would prefer to first select another component (for example, HttpFoundation and HttpKernel).

As @Cerad suggested you wrap your old code in Symfony. Check out the IngewikkeldWrapperBundle bundle. You cannot use it as is, but it can give you some ideas.

There is a third way.

You can implement every new feature in a Symfony application. Then you can make both the Symfony apps and the apps coexist. At the server level (i.e. Nginx), you can proxy obsolete URLs for the legacy application and all migrated URLs to the Symfony2 application. In my case, this scenario was the best option and turned out to be workable. However, we tried to abandon the development of obsolete applications (therefore, every new feature or change should have been developed in the Symfony2 application).

Edit: this is how you could load the Symfony core in an outdated application and send an event (which is necessary for the firewall):

 $kernel = new \AppKernel('dev', true); $kernel->boot(); $request = Request::createFromGlobals(); $request->attributes->set('is_legacy', true); $request->server->set('SCRIPT_FILENAME', 'app.php'); $container = $kernel->getContainer(); $container->enterScope('request'); $container->get('request_stack')->push($request); $container->set('request', $request); $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); $eventDispatcher = $container->get('event_dispatcher'); $eventDispatcher->dispatch('kernel.request', $event); 
+10
source

I believe that you can access the container instance from your old application, for example

 $kernel = new AppKernel('prod', true); $kernel->loadClassCache(); $kernel->boot(); $request = Request::createFromGlobals(); $container = $kernel->getContainer(); $sc = $container->get('security.context'); 
+1
source

All Articles