tags:

views:

335

answers:

2

I have a prototype website written in PHP. Lately i've rewritten code to separate logic from layout and database operations. So now I have something like MVC code design.

Now what bothers me, is that in MVC I'll have many files and each will display something when combined with other (model+view+controller). So I figured out that it would be usefull to create something like widgets of my MVC parts.

For example when I have MVC of products view than I would create a PHP file that combine MVC part files in such fashion to display products based on GET values I pass to this new PHP file.

What I would acomplish in this way is that any widgets, subparts of website would be available to view separately and use separately from other parts. So it would be something like widgets or like Firefox browser design.

So I would be able to test every part of website separately, and than only combine these parts into current actual website page design. So user testing would be easier too, and the presentation files would be very short and easy to understand.

I would like to know what do you think about it. I don't want to fall into any hole in the design-things and I think that this is the moment that will be important in future to keep maintaining website code easy.

Am I correct?

+1  A: 

MVC I'll have many files and each will display something when combined with other (model+view+controller)

This sounds like you didn't get the MVC-model right. Only Views have content or 'display something'.

Based on this, you can create actions which only create a small widget-like part of content, which then can be combined in your Layout.

I'd like to recommend reading the Zend_Layout Quick Start.

Karsten
+1  A: 

The answer to your problem is view composition.

I suggest you splitting the whole screen into smaller pieces: top bar, second-level nav bar, products list, preview area, and so on. These could be provided by separate views, view helpers or additional methods in your controller. Then a controller action would compose pieces into single screen using simplistic HTML layout.

Eg. for top bar that is static this could be specialised view. For products list that is dynamic it could be controller method getProductsList($categoryId) that provides complex view. Product preview can be delivered by view helper. See the following example:

// inside class ProductsController
public function index($categoryId, $productId = null) {

    // specialised view
    $topBar = new TopBarView();
    $topBar->selected = 'products';

    // helper method
    $list = $this->getProductsList($categoryId);

    // helper object
    $previewHelper = new PreviewHelper($productId);
    $preview = $previewHelper->getView();

    // view composition
    $view = new View('path/to/template.tpl');
    $view->add($topBar);
    $view->add($list);
    $view->add($preview);
    return $view;

}

This is just an example to illustrate how composition works.

Hidden benefit of having method for delivering widget (i.e. products list) is that it could be reused to support Ajax. Changing category would require Ajax call to getProductsList method with new category id.

Michał Rudnicki