views:

102

answers:

3

At work we currently use a pretty outdated (php) "framework" that doesn't really make use of all the cool new stuff like spl autoloading, mvc, pretty much all the new stuff that came out for PHP in the last 2-3 years.

I'm trying to transition this framework slowly into a more modern mvc based one but I don't want to jump the gun and completely switch things up, but make a slow slow transition.

Right now, the way it works out is ( and I'm sure many of you loathe this or have done this before ) - for every .php page on the site we have a file and that points to a master.php ( the reason we still have these flat .php pages that point to master.php is because sometimes but not very often we have to place these sites on IIS which doesn't support mod_rewrite and I believe it's 5 or 6, not 7 ). In master.php we have a bunch of includes for css, js, and then in the content area we have a bunch of switch statements that act on the page URL, eg:

<head>
switch ($pageName ) {
   case 'specialPage': echo $databaseContent->grabRSSTitle(); break;
   default: echo $xml->printContent( $pageName ); 
}
</head>
<body>
<div id=content>

<?php
switch ( $pageName ) {
   case 'contact-form': include 'includes/contact-form.php'; break;
   default: echo $xml->printContent( $pageName);
}
?>
</div>
</body>

Keep in mind there are a lot of custom areas and not just those two examples which I typed, so I'm trying to also keep the flexibility there.

So if we were to hit up 'sitename.com/contact-form.php' it would first go to that file, which then includes the master.php file, does the switch on $pageName which is 'contact-form' and includes relevant portions for the content area.

By the way, we do store content in an xml based format instead of relying on databases - it just seems to be much easier making edits in xml locally than a database. I'm wondering if this is even practical as pretty much everywhere I go I see a database being used for this. ( We have seperate elements inside these xml files for things such as meta area, content area and spit them out seperately from within the master.php file ).

I'm wondering if anyone has tips on how to meld this into an mvc based one slowly, so that others aren't completely lost. Or is it even worth it? Should I go with a popular framework like Kohana/Zend?

+2  A: 

My opinion is that you should just dive straight in to the deep end rather than making a slow transition... I say this because I feel you'd actually benefit more by translating the pieces of your existing code into the framework of your choosing rather than migrating your existing code to a more home-brewed MVC. This is because most of the popular frameworks have been "battle hardened" and already have solid solutions to a lot of the problems and challenges you're likely to encounter during a migration (nor is there really any reason to re-invent the wheel ;).

Now, all that being said, I think there's a lot to be learned from rolling your own MVC framework if you've got lots of time to do so, but I still think your best bet is to pick a framework and rebuild your functionality with that from scratch.

As far as frameworks go, there are a lot of good ones out there, you really should pick one that appeals most to you. There are tons out there, but these are the ones I seem to see used most frequently (not to say these are the best out there):

I'm sure others will throw their suggestions up as well :) Hope that helps, or at the very least provides some food for thought.

Ian Selby
+1  A: 

Like Ian said, I think you should just dive in. Depending on the size of the project, and the time you've set aside for the transition, this is the best way to approach it.

If you don't mind me recommending something I've written, check out EuropaPHP (http://europaphp.org/). It's very, very fast and allows you to drop-in the Zend Framework without any configuration due to the fact that follows very similar coding and naming conventions.

My contact info is on the site, so feel free to contact me with any questions that you may have, given you decide to give it a go. I'm in the process of writing tutorials for it, but it's very well documented and there is a quick-start guide that'll help you get started. I've gotten some pretty good feedback on it so far, but am open to suggestions if you or anyone else has any.

There are many good frameworks out there. I prefer, the Europa/Zend combo for both speed and libraries, but it all depends on your coding style.

Tres
+2  A: 

I would recommend The ZEND framework, it allows you to use as much or as little of the MVC principles as you feel you need to allowing a nice easy transition. Some of my devs picked Zend up in 2 days, others took 2 weeks, it's easy once you get into the MVC mindset.

Looking at you code you should be able to reuse most of it and then move over to Zend Db classes etc as you get more familiar.

Basically, after installing zend of course, you would start with the main controller, indexController.php. Zend decides which action function to run in that controller class based on the URL, so if you passed in http://yousite/index/contacts Zend knows to run the contactsAction function inside indexController.php

All of your shared startup code can now be run in the constructor of indexController and the per page logic and output can be done in the the controllers corresponding member function

Zend has awesome classes built in for just about everything, sending an email can be done in 5 lines of code for instance, and it comes with the dojo javascript library which allows you to do all the ajax and UI stuff with style.

My devs always struggle to understand where things live in relation to each other, and how does zend know what to call: If you understand this next bit you are halfway there:

The controller is the chunk of code that runs first, it's job is to get all the data ready for the view so that it can lay things out. In the example above, the indexController's contactsAction function passes data to it's view as $this->view->foo = "bar". The actual view script for that function lives inside the directory views/scripts/index/contacts.phtml, and it has access to the same foo variable that the controller populated as echo $this->foo;

I recommend Zend Studio for Eclipse for the IDE

Don't be scared to get those feet wet, the waters fine :)

Gary Benade
Well, it's not me in particular because I've done a little Zend, Pylons, Django but just some of the other members on my team that aren't as interested and just want to get the job done in the last amount of time.What resources would you say are the best for Zend, other than the official docs?
meder
Gary Benade
Zend's Developer Zone is a good resource besides the offical docs. http://devzone.zend.com/public/view
Tres
@soulscratch I had the same problem at my last job. Your teammates are likely unwilling to learn a new system because its comfortable. But if all they want to do is get the job done asap, then the Zend library should be reason enough to switch over. It can save you hours over the course of any given week.
Justin Johnson