zend-framework2zend-layout

ZF2 how to display tweets in layout


I'm developing a site using ZF2. One of the section in footer contains tweets. I can fetch tweets but I am not sure how to display them in the layout. I have following ideas but not sure which approach to use

  1. Create a controller plugin and use it on each request.
  2. Create a ViewHelper which fetches the tweets from the ServiceLocator and call the helper in the layout
  3. Attach an event to the MvcEvent::EVENT_RENDER in the module.php which sets the html to a layout variable.

If there is anyother better approach, please let me know.


Solution

  • What you are refering to completely is the task of a ViewHelper. So your second approach would be correct. You have two layers here

    ServiceLayer - fetching the tweets from a DB / Twitter / DataSource

    ViewLayer - displaying whatever the ServiceLayer give you

    Now the thing is what do you want to do? Do you want to have a ViewHelper that simply renders a number of given Tweets? Then you'd not need access to the TweetService itself, but rather the given Data. Something like this:

    'view_helpers' => array(
        'factories' => array(
            'tweetRenderer' => function ($vhm) {
                $tweetService  = $vhm->getServiceLocator()->get('my-tweetservice');
                $tweetRenderer = new \My\View\Helper\TweetRenderer();
                return $tweetRenderer->render(
                    $tweetService->getAllTweets()
                );
            }
        )
    )
    
    // in your view then:
    echo $this->tweetRenderer();
    

    But most commonly such a thing isn't really that handy. You'd probably want to be able to at least change the username of the Tweets to render. So you'd need to inject the actual TweetService into your ViewHelper and then use the __invokable() function of the ViewHelper to set the parameter for the Renderer:

    'view_helpers' => array(
        'factories' => array(
            'tweetRenderer' => function ($vhm) {
                $tweetService  = $vhm->getServiceLocator()->get('my-tweetservice');
                return new \My\View\Helper\TweetService($tweetService);
            }
        )
    )
    
    // TweetService.php
    public function __construct(TweetServiceInterface $tweetService) {
        $this->tweetService = $tweetService;
    }
    
    public function __invoke($username) {
        return $this->render(
            $this->tweetService->fetchTweetsForUser($username)
        ):
    }
    
    // in your view then:
    echo $this->tweetRenderer('manuakasam');
    

    And that's the basics there are :)