Home » Php » php – Migrating from Spaghetti code to Symfony 2

php – Migrating from Spaghetti code to Symfony 2

Posted by: admin July 12, 2020 Leave a comment


I just got a job back in corporate america, and I have inherited a nasty bunch of php 4 flat files. I want to migrate to Symfony 2, but there is no way I’ll get permission to re-factor the whole thing.

Does anyone have any suggestions for migrating small parts or sections of a project to Symfony 2? Perhaps I could have a directory with the entire old code base and start by only using the routing in Symfony 2 to grab the old flat files?

If anyone has any suggestions I’d love to hear them.

How to&Answers:

First of all, have you asked whether they’ll let you refactor the application? You can build a business case based on:

  • Ability to maintain the software going forward at a lower cost
  • It’ll be easier to introduce other developers to the project
  • It’ll be easier to add new features

A full re-factor may not be as complicated or time consuming as you envision, and it can be done in parallel while maintaining the old app.

Now, doing what you suggest is a bit more tricky. You have to consider the following:

  • Being able to use an ORM on the existing database structure depends on how well it’s designed.
  • A lot of php4 code will not work on php5 and you may end up changing a significant amount of code, introducing bugs, etc.
  • Some custom code will be required in order to integrate the session on both code-bases.

If the above points are not an issue, you may get away with adding the php4 structure to the web folder so that they can be accessed directly, and start replacing routes one by one as you build each screen out in Symfony.

These suggestions are very contextual. It really depends on how complex your application is, and how messy it really is.

Good luck tho!


First of all, put a custom made front controller as a front-end to those flat files.

You can redirect old urls to the new front controller with mod_rewrite.

RewriteRule ^/?(.*) /index.php/$0
# this shall work in .htaccess (/paths) and in httpd.conf (non slashed local paths)

Paths can then be retrieved in index.php trough $_SERVER['PATH_INFO']

From the front controller you can handle some fine grained gradual redirections to any canned solution you wish to move to. For the URLs for whom you want to preserve old functionality, some include("./".$_SERVER['PATH_INFO']) shall do the trick.


My advice is to break it out into steps. Often management will accept a series of small cost much more readily than a one time big cost. I’d focus on getting the code cleaned up and PHP5 ready before migrating to Symfony2.

Once the code is clean, integrating Symfony2 into your project should be pretty easy.