Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the Symfony 2 Container in a legacy app

Would like to integrate a legacy application with a Symfony 2 application - replacing more and more parts of the old application with Symfony components. The approach I would take is using the Symfony 2 container in the legacy application getting the 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 this feasible?
  • How do I get the configured service container?

More info in the legacy application: The typical PHP mess: Single PHP files, as "controllers" (checking $_GET and $_POST for different execution paths). Each page includes init.php which sets up autoloading, database connection etc. The session management has its own class (which i would like to replace), the data is retrieved through calls to static methods (!) of database objects.

like image 568
chiborg Avatar asked Apr 16 '12 08:04

chiborg


People also ask

How to ask for a service in Symfony container?

The moment you start a Symfony app, your container already contains many services. These are like tools: waiting for you to take advantage of them. In your controller, you can "ask" for a service from the container by type-hinting an argument with the service's class or interface name. Want to log something? No problem:

How do Symfony type-hints work?

When you use these type-hints in your controller methods or inside your own services, Symfony will automatically pass you the service object matching that type. Throughout the docs, you'll see how to use the many different services that live in the container.

What is a Symfony service?

Almost everything that your app "does" is actually done by one of these objects. And each time you install a new bundle, you get access to even more! In Symfony, these useful objects are called services and each service lives inside a very special object called the service container.

How do I run Symfony from a docker container?

$ docker build -t symfony-tutorial. The final step is to run the container you have just built using Docker: $ docker run -it -p 8000:8000 symfony-tutorial The command tells Docker to run the container and forward the exposed port 8000 to port 8000 on your local machine.


1 Answers

Using Symfony's DIC as a standalone component is possible but you'd have to do many things "manually" (as you're not planning on using full Symfony Framework from the very beginning). You'll probably won't get much of using DIC with all that legacy stuff.

If you want to go this path I'd consider choosing another component first (like HttpFoundation and HttpKernel).

As @Cerad suggested you might wrap your legacy code in Symfony. Have a look at IngewikkeldWrapperBundle bundle. You can't use it as is but it might give you some ideas.

There's a third way.

You can decide to implement every new feature in a Symfony app. Than, you can make that both legacy and Symfony apps coexist. On a server level (i.e. Nginx), you might proxy legacy URLs to the legacy app and all the migrated URLs to a Symfony2 app. In my case this scenario was the best option and proved to be working. However, we were committed to abandon legacy app development (so every new feature or change had to be developed in a Symfony2 app).

Edit: here's how you could boot the Symfony kernel in a legacy app and dispatch an event (which is needed 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);
like image 85
Jakub Zalas Avatar answered Sep 21 '22 07:09

Jakub Zalas