POSTS
Migrating progressively to Symfony without pain
By Carlos Buenosvinos
- 3 minutes read - 609 wordsAtrápalo is a travel e-commerce website founded in 2000. Based in Barcelona, Spain, it sells flights, trips, tickets, booking restaurants, car renting, etc. to 10 different countries. It’s a 9000 world Alexa ranking and it’s running PHP. Since 2014, we are pushing hard in order to evolve technically using best practices, agile methodologies and distributed architectures. One of the key aspects is the framework.
We are currently migrating to Symfony in order to speed up the development process and reduce the maintenance costs. We are doing it progressively, step by step, without rewriting the whole application, no green-field project, without any dedicated team neither. All developers are involved in this process, and by policy, each new feature is developed using Symfony while the old features remain served by the old framework.
I would say this process is going quite smoothly, without pain. Based on some emails and tweets I have received, here are some tricks about how we are doing it. Hope it helps!
tl;dr
In our app, Symfony and our current framework live together. Some requests are served by the old framework and the new features are served by Symfony, by department policy. An easy approach to do that without pain is to use the Apache dumper feature of the Symfony Router component. When deploying, an Apache config file is generated that sends all the Symfony @Route to the app_*.php file and the rest to the index.php of old framework. Done!
Entry points
Let’s assume you have your own custom framework. We have one of those. It has a single entry point, an index.php file that manages all the incoming requests. An example of an Apache configuration file or .htaccess would be like:
Imagine for a sec that we could add some rules that would send to the Symfony app_*.php some request if they match your @Route regexp. For example, the routes related to the profiler:
With this approach is really easy to dispatch requests to one framework or the other, but, how can I generate these rules?
Generating the Symfony routes
With the Apache dumper feature of the Symfony Route Component. This feature is deprecated but you should be able to copy&paste the command and tune it for your goal if you need it.
If you don’t want to use this command, you can write your own, but it’s going to be a bit tricky.
There is no nginx dumper, but it shouldn’t be difficult to write one.
The existing command was thought to improve the performance of the Symfony apps running on Apache. Now, with PHP and Symfony improvements, it’s not so necessary this approach, but interesting for the migration one.
Other details
We are using Hexagonal Architecture, sharing the same Symfony Container, so reusing the Business logic between frameworks is easy for us. Using this technique is almost mandatory in a step by step migration process. Each time we deploy, we generate the Apache file.
Alternatives
I have seen people trying to integrate Symfony routing with its own routing and failing. It could work, but it’s quite work. With the approach explained here, it’s just a matter of your webserver. Another option, should be Symfony making curl requests to the old framework. The last one seen, is using both frameworks as a middleware of StackPHP. We have tried the last one but no success.
Show me some code
Just as an example. First of all the “copy&paste” of the router:dump of Symfony with some tweaks. You have the original version here. It uses another dumper and customises the path to generate the configuration file.
And now the dumper that generates the configuration file with all the routes.