Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Porting a non-framework PHP project to Laravel 4.x

I have a big PHP project in which I didn't use any framework (nor MVC pattern), and I want to gradually port it on Laravel with (almost) no downtime.
I'm searching for ways of making transparent the migration for pieces of code already ported to Laravel, while mantaining functional older plain PHP code.

For example, Laravel "overwrites" superglobals ($_GET, $_POST, etc.) using classes and methods, (ex. Input::get()). My plain PHP project of course makes direct use of superglobals. How can I overcome these "uncompatibilities" under Laravel without having to rewrite instantly all my PHP code?

Otherwise, if you think this task being too difficult, is there any PHP framework that, thanks to its internal structure, would make this task easier?

UPDATE:
The use of superglobals is still possible in laravel. I was getting a sneaky error: Laravel internally sets error_reporting to E_ALL and shows a custom error stack trace page even for PHP E_NOTICE, but without explicitly specifying the error level (that it is a NOTICE error), even if this was done by default by PHP error reporting message engine.

Let me say, for Laravel core developers, that I consider this "partially silent" behaviour as generally misleading for any PHP developer trying to port his code on to their framework.

like image 965
Matteo-SoftNet Avatar asked May 05 '14 07:05

Matteo-SoftNet


1 Answers

TL;DR

Don't do this.

Common misconception

If you have an existing project, then grafting it on a framework will gain you no benefits. Frameworks are not some magic sauce, which miraculously makes the code better or make the site run faster.

Instead, frameworks are tools, that (supposedly) help you write the project in a shorter amount of time by having the "common tasks" already done. Also, as a side effect, it is extremely difficult to stop using a framework in a project (if it, for example, gets discontinued), because all of your code now is tightly welded to a framework.

This is why you are currently having these problems:

  • How can I overcome these "uncompatibilities" under Laravel without having to rewrite instantly all my PHP code?

    You cannot. Rewrite it exactly as the Laravel framework wants you to or it won't work.

  • (..) is there any PHP framework that, thanks to its internal structure, would make this task easier?

    No, there are not. All the popular frameworks will want you to rewrite your code so that it is bound to a framework.

The better approach

But you already have a working application. You will have better results, if you focus on improving the existing code base:

  • make sure you follow SOLID principles
  • separate your business logic from you presentation and database abstraction
  • start adding unit tests for the really shady parts of your code
  • refactor, refactor, refactor

P.S.: the materials listed here wold probably help

like image 191
tereško Avatar answered Sep 20 '22 01:09

tereško