PHP code example of foa / responder-bundle

1. Go to this page and download the library: Download foa/responder-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

foa / responder-bundle example snippets



// BlogBrowseAction.php
namespace Vendor\Blog\Web\Action;

use FOA\DomainPayload\PayloadFactory;
use Vendor\Blog\Web\Responder\BlogBrowseResponder;

class BlogBrowseAction
{
    protected $domain;
    protected $responder;

    public function __construct(
        BlogService $domain,
        BlogBrowseResponder $responder
    ) {
        $this->domain = $domain;
        $this->responder = $responder;
    }

    public function __invoke()
    {
        $payload_factory = new PayloadFactory();
        $payload = $payload_factory->found(
            array(
                array('title' => 'Some awesome title', 'author' => 'Hari KT'),
                array('title' => 'Some awesome post', 'author' => 'Paul M Jones'),
                array('title' => 'Some awesome content', 'author' => 'Justin'),
            )
        );
        // Rather than the above code, you should actually do something like
        // $payload = $this->domain->fetchPage($page, $paging);
        $this->responder->setPayload($payload);
        return $this->responder->__invoke();
    }
}


// BlogBrowseResponder.php
namespace Vendor\Blog\Web\Responder;

use FOA\Responder_Bundle\AbstractResponder;

class BlogBrowseResponder extends AbstractResponder
{
    protected $available = array(
        'text/html' => '',
        'application/json' => '.json',
    );

    protected $payload_method = array(
        'FOA\DomainPayload\Found' => 'display'
    );

    public function display()
    {
        if ($this->negotiateMediaType()) {
            $content_type = $this->response->content->getType();
            if ($content_type) {
                $view .= $this->available[$content_type];
            }
            $this->renderView('browse', 'layout');
        }
    }
}


$factory = new \Aura\Html\HelperLocatorFactory;
$helpers = $factory->newInstance();

$engine = new \Aura\View\View(
    new \Aura\View\TemplateRegistry,
    new \Aura\View\TemplateRegistry,
    $helpers
);

$renderer = new \FOA\Responder_Bundle\Renderer\AuraView($engine);


$web_factory = new \Aura\Web\WebFactory($GLOBALS);
$response = $web_factory->newResponse();

$accept_factory = new \Aura\Accept\AcceptFactory($_SERVER);
$accept = $accept_factory->newInstance();

$responder = new \Vendor\Blog\Web\Responder\BlogBrowseResponder($accept, $response, $renderer);


use FOA\DomainPayload\PayloadFactory;

$payload_factory = new PayloadFactory();
$payload = $payload_factory->found(array('name' => 'Hari KT'));
$responder->setPayload($payload);

$responder->__invoke();


echo $response->content->get();

$di->params['FOA\Responder_Bundle\Renderer\AuraView']['engine'] = $di->lazyNew('Aura\View\View');
// responder
$di->params['FOA\Responder_Bundle\AbstractResponder']['response'] = $di->lazyGet('aura/web-kernel:response');
$di->params['FOA\Responder_Bundle\AbstractResponder']['renderer'] = $di->lazyNew('FOA\Responder_Bundle\Renderer\AuraView');
$di->params['FOA\Responder_Bundle\AbstractResponder']['accept'] = $di->lazyNew('Aura\Accept\Accept');


namespace FOA\Responder_Bundle\Renderer;

use FOA\Responder_Bundle\Renderer\RendererInterface;
use Your\TemplateEngine;

class YourTemplateEngine implements RendererInterface
{
    public function __construct(TemplateEngine $engine)
    {
        $this->engine = $engine;
    }

    public function render($data, $view, $layout = null)
    {
        // according to how the rendering engine works. See other implementations
        // $this->engine->render
    }
}