Something in between MVC and CMS
Posted: Fri Mar 16, 2007 3:18 pm
The other day I created a web site for hosting a video (a frequent task in my job) but this one was slightly different in that
Whilst being a slightly different project from those in the past, for those reasons I've just explained, it was also similar in that, like before, this new website needed to be done in the look and feel of the client's company and had to be expandable for more videos later - that means sections, subsections and navigation. In order to get the project out I pretty much ignored that on a functional level; ensuring that the site's design appeared to have room to grow but actually doing what ever was necessary to get the code up and running in the time. That is to be expected and wasn't a real problem.
Once the project was successfully completed the client was very pleased. I recommended to my boss that it should be "refactored" so that we can respond to requests for expansion quickly. He listened while I explained what refactoring is and why it is necessary; making the points that not refactoring leads to code that get exponentially harder to maintain and how it's a bit like how cell references in excel can make changing a value a very quick task compared with having the that value duplicated across the worksheet.
By the time I'd finished explaining all this another member of staff involved in the project walked in the room and that was great because we got to explain to him as well about refactoring. A few short moments later it was agreed that the refactoring should be done and in fact the decision was a "no brainer", to which I completely agree. And so begun my job of refactoring the code.
So how should such a project be refactored?
I spent a few moments considering a couple of strategies. What I really wanted to do was to write the site up in a meaningful XML structure and use XSLT to transform it into HTML. But currently I can't used XSLT because of server limitation and besides how am I going to integrate forms into that? So I decided on MVC and the Zend Framework. I spent a whole day setting up ZF, SimpleTest, PHPUTF8, writing controllers, models, views, etc. I was determined to do it as perfectly as possible because this project would be a model of future ones. Towards the end of the day I was starting to realise that it wasn't as easy as it should be. In fact it was significantly harder than the unrefactored version, which, although a little messy, was pretty easy to follow. Worryingly I ended up with a controller page that looked a bit like this:only with a lot more assignments to $view and much longer strings. The view main.php was then handling all of these different assignments and many more with the idea of being as powerful as possible. I even extended Zend_View to allow for nested views so that I could contains views within view and reap some flexibility from view specialisation.
MVC is designed to "decouple data access and business logic from data presentation and user interaction" and was doing a fine job of it but clearly that isn't what I'm after. I was misapplying the pattern completely.
So this got on to thinking about a CMS. CMSes "facilitate the organization, control, and publication of documents and other content, such as images and multimedia resources" and sounds a little more like what I'm after but then how do I integrate custom functionality such as my video chooser wizard thing into that. Incidentally I've no intention of using a pre-packaged CMS or writing anything myself much larger than is necessary to satisfy the requirements of this site but it sounds to me that I'm after some kind of CMS / MVC hybrid or at least a template system that blocks of functionality can be injected into or perhaps the other way round.
Hope you enjoyed the story and have lots of useful suggestions for me
- I had to turn the whole thing out in about 9 hours
- The video being hosted came in several different flavours (two different quality streaming pages, three different download qualities and a fourth download for burning to DVD)
Whilst being a slightly different project from those in the past, for those reasons I've just explained, it was also similar in that, like before, this new website needed to be done in the look and feel of the client's company and had to be expandable for more videos later - that means sections, subsections and navigation. In order to get the project out I pretty much ignored that on a functional level; ensuring that the site's design appeared to have room to grow but actually doing what ever was necessary to get the code up and running in the time. That is to be expected and wasn't a real problem.
Once the project was successfully completed the client was very pleased. I recommended to my boss that it should be "refactored" so that we can respond to requests for expansion quickly. He listened while I explained what refactoring is and why it is necessary; making the points that not refactoring leads to code that get exponentially harder to maintain and how it's a bit like how cell references in excel can make changing a value a very quick task compared with having the that value duplicated across the worksheet.
By the time I'd finished explaining all this another member of staff involved in the project walked in the room and that was great because we got to explain to him as well about refactoring. A few short moments later it was agreed that the refactoring should be done and in fact the decision was a "no brainer", to which I completely agree. And so begun my job of refactoring the code.
So how should such a project be refactored?
I spent a few moments considering a couple of strategies. What I really wanted to do was to write the site up in a meaningful XML structure and use XSLT to transform it into HTML. But currently I can't used XSLT because of server limitation and besides how am I going to integrate forms into that? So I decided on MVC and the Zend Framework. I spent a whole day setting up ZF, SimpleTest, PHPUTF8, writing controllers, models, views, etc. I was determined to do it as perfectly as possible because this project would be a model of future ones. Towards the end of the day I was starting to realise that it wasn't as easy as it should be. In fact it was significantly harder than the unrefactored version, which, although a little messy, was pretty easy to follow. Worryingly I ended up with a controller page that looked a bit like this:
Code: Select all
public function indexAction()
{
$view = Zend::registry('view');
switch ($this->_getParam('section', 'foo')) {
case 'foo':
$view->pageTitle = 'foo';
$view->body = 'some body';
switch ($this->_getParam('subsection', 'zim')) {
case 'gir':
$view->videos = array(new Video(...));
break;
case 'zim':
$view->videos = arrat(new Video(...), new Video(....));
$view->xtraBlurb = ';lkdfs';
break;
default:
$view->xtraBlurb = 'dfsdgf';
}
break;
case 'bar':
$view->pageTitle = 'The very important bar';
$view->body = 'blah blah. I\'ve drunk too much at the bar';
switch ($this->_getParam('subsection', 'dib')) {
case 'dib':
$vide->videos = array(new Video(...));
case 'gaz':
$vide->videos = array(new Video(...));
default:
$view->body = 'stuff';
}
break;
default:
}
echo $view->render('main.php');
}MVC is designed to "decouple data access and business logic from data presentation and user interaction" and was doing a fine job of it but clearly that isn't what I'm after. I was misapplying the pattern completely.
So this got on to thinking about a CMS. CMSes "facilitate the organization, control, and publication of documents and other content, such as images and multimedia resources" and sounds a little more like what I'm after but then how do I integrate custom functionality such as my video chooser wizard thing into that. Incidentally I've no intention of using a pre-packaged CMS or writing anything myself much larger than is necessary to satisfy the requirements of this site but it sounds to me that I'm after some kind of CMS / MVC hybrid or at least a template system that blocks of functionality can be injected into or perhaps the other way round.
Hope you enjoyed the story and have lots of useful suggestions for me