Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strategies for migrating live site to MVC structure?

There is a lot of good content on SO about MVC and getting started with MVC, but I'm having trouble finding anything about how best to implement MVC structure on a pre-existing, live website.

My site is a nasty mishmash of echos and concatenated HTML that would make any professional programmer throw-up, but it works.

I'd like to spend some time tackling the mounting technical debt, however, and that means moving to a much more sane MVC structure.

If at all possible, I'd like to avoid a let 'er rip! 100% rewrite and launch approach, and instead take it a section at a time. But it seems the basic controller's centralized structure is not suitable for such an approach?

like image 305
Drew Avatar asked Oct 18 '11 06:10

Drew


2 Answers

If i understand what is the overall level of quality for that codebase , then there is no way to move to MVC in one step. It is just impossible. Another bad news is that frameworks will not help. They cannot magically transform a bad codebase into something resembling MVCish architecture.

Instead you should focus on incremental refactoring. Your goal should be code that is mostly following SOLID principles and LoD. And while you refactor your code , the architecture will emerge by itself. MVC has many variants and flavors.

One definite thing you might want to look at are the ways of using templates in php. Examine the code, and see what you has to change to fit your needs (it is more of a direction, not a complete solution). And remember that in MVC-like structures View is not a template, but it View uses multiple templates.

Another thing you might benefit from is learning more about datamappers. Implementing them would be a good step in direction of creating model layer.

Oh .. and then there are few general lectures you could take a look at ( all are 30min+ ):

  • Advanced OO Patterns
  • Clean Code I: Arguments
  • Clean Code III: Functions
  • Don't Look For Things!
  • Global State and Singletons
  • Inheritance, Polymorphism, & Testing

Oh , and this book has some insights into refactoring large php projects. Could be useful for you.

like image 148
tereško Avatar answered Oct 13 '22 00:10

tereško


i agree with the other suggestions here, a framework isn't going to be a magic fix.

however it can help in the long run. i have converted a number of mishmash sites to kohana framework now, and have had the following experiences.

initially i didn't know kohana well enough, so i was learning that while recoding mysite. I ended up stopping the rewrite and coding a completely new project from scratch to learn kohana, then went back to the rewrite project, now that i understood the framework better.

if you don't understand the framework, it is going to be a steep learning curve trying to use it to convert an old project

  1. first step in the rewrite was to pull all the business/database logic embedded into the pages up to the top of each page (prior to the html output). So that i wasn't changing the flow/structure of the website, just separating business logic from display logic.

    After that i had a site that had easily readable business logic, just in the old structure, and i had familiarised myself with the old codebase at the same time.

  2. next step i did was to fix any database structure issues so that everything was in 3rd normal form (if possible).

    i found it easier to modify the old code to the new database structure, then to work around and old database structure in the new framework. (kohana is largely a convention based framework, rather then configuration, so it was nice to follow those conventions, to ease long term maintenance)

    having a good database structure makes life easier regardless of framework

  3. next step was to pick a part of the website to replace. set up the routes in kohana and let kohana serve that part of the project. kohana (and other frameworks no doubt) have a fallback, if a file being requested via a url already exists on the site, then kohana won't handle that request

    since you have separated the business logic from the display logic in your php files, it is a simple matter of splitting the code into a controller and a view. make the changes to both parts to suit the framework. you can split the business logic into model/controller after you have the controller/view working as expected

work your way through that part of the site, until complete. then test/launch/bugfix etc

then start again on the next part of the site.

eventually you will get there...

although it took a lot of time to rewrite, for me it was worthwhile, as the sites are far easier to maintain now. (obviously the amount of gain will be dependant on the quality of the original codebase)

good luck

like image 33
bumperbox Avatar answered Oct 13 '22 01:10

bumperbox