views:

239

answers:

2

I am in the early stages of creating a small-medium sized web application by myself. I read "PHP Objects, Patterns, and Practice," and I decided to use page controllers because quick production is a high priority. Page controllers were also appealing because I am unfamiliar with PHP frameworks and creating an elaborate front controller seems to currently outweigh the project. My plan is to release the page controller version of the website ASAP and see if the audience likes it before committing to more complex software designs.

That said, I might decide to add more 'modules' and developers down the road...at which point I really should switch to a front controller. The above mentioned book describes that "it is not impossible to start with page controller and move toward the front controller pattern," but the wording of "not impossible" has me worried that it may be fairly difficult.

My questions are: How 'hard' is it to make the transition from page controller to front controller design? While working on my page controller-based app, what should I be careful with to create code that allows a smooth transition to front controller design? The ideal situation would be to rewrite/restructure the code as little as possible and tack on the front controller with related classes/objects. Right now I'm only cautious about maintaining MVC, so any advice from experienced developers would be great. Thank you.

+1  A: 

I think your main concerns should be to put "business logic" into properly designed model classes. In other words, don't put such logic in your page controllers directly. Instead, use your page controllers to parse input, such as GET and POST, instanciate the correct business logic objects, and call the correct methods on those to do the actual data crunching.

The page controllers should also be responsible for choosing a view to display - don't output things in the business objects, simply return data for the controller.

By using this approach, you should have most of your code in the model classes, and if/when you want to rewrite to using front controller, you will only need to change the page controllers to work with a front controller, hopefully not having to modify anything else.

Jani Hartikainen
+1  A: 

I read the book you mentioned about 3 or 4 years ago as a first introduction to object oriented programming and I found it a great book, but I have to say that I only really got to grips with the whole thing once I started to use CakePHP and root around in its code to see how things are done in practice. The examples in the book can be a little confusing at times.

I recommend having a second look at some of the frameworks out there, Cake is good, Yii is meant to be even better. It doesn't long to get a system set up with them, and as long as you're prepared to have a good look under the hood, they can be an invaluable learning tool.

thief