Tobias
Tobias

Reputation: 81

zf2 extend existing modules

I've been working with zf2 for a while but i could not get a solution for the following problem.

eg. we have a module called "Product" which handle the most of the product stuff. Another module - let's call it "Review" - should extend the Product module and extend the product view to offer a product review to the user.

Until this point there seems to be no problem. We can easily overwrite the product view in the Review module.

Now the tricky part where i got stuck. There's a third module - let's call it "Social". This module should provide social functioniality to the whole application. This module should also modify the product view and add a link in the product page called "EMail to a friend".

Now my problem... The product view is modified by the review module. If i overwrite the view in the Social module, the changes from the Review module are lost.

// Edit Info: The Social and Review module should be able to modify the views WITHOUT modifiing the Product module.

Any tips, hints or ideas are welcome.

Upvotes: 0

Views: 123

Answers (1)

AlexP
AlexP

Reputation: 9857

It depends what code you are trying to avoid duplicating.

If it's just HTML content you can create a custom ViewHelper to render the required HTML. This could then be reused within each view that needs the "social" content.

I suspect however you are reffering to the a 'social' controller action and you wish to reuse the return result of that within other views. If so, one soultion would be to use the forward() controller plugin.

From the documentation:

Occasionally, you may want to dispatch additional controllers from within the matched controller – for instance, you might use this approach to build up “widgetized” content. The Forward plugin helps enable this [by] returning the results of the dispatched controller action

This is useful in a situation like yours as you require the "Social" module to be an additional element of the view; rather than a replacement.

For example

// SocialModule\Controller\SocialController.php
// Controller action to display social buttons (twitter/facebook etc)
public function socialAction()
{
    // Some controller logic...

    // Returns the social button view
    return new ViewModel(array('foo' => $bar));
}

// ProductModule\Controller\ProductController.php
public function productViewAction()
{
    $product = $this->productService->find($this->params('id'));

    // Displays the product 'view' page
    $view = new ViewModel(array(
        'product' => $product,
    ));

    // Re-disptach the social module action and return it's view model
    $socialView = $this->forward()->dispatch('SocialModule\Controller\Social', array(
        'action'  => 'social',
    ));

    // We now have the view model, attach it to our view
    $view->addChild($socialView, 'social');

    // Return the aggregated view
    return $view;
}

All then is required is to render the content in the view

// product-view.phtml
echo $this->social;    

Upvotes: 1

Related Questions