PHP code example of portrino / typo3-fractal-view

1. Go to this page and download the library: Download portrino/typo3-fractal-view 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/ */

    

portrino / typo3-fractal-view example snippets


use Portrino\Typo3FractalView\Mvc\View\FractalView;
use Foo\Bar\Transformer\BookingTransformer;

class BookingController
{
    /**
     * @var array
     */
    protected $viewFormatToObjectNameMap = [
        'json' => FractalView::class
    ];
    
    /**
     * @var string
     */
    protected $defaultViewObjectName = FractalView::class;
    
    /**
     * Action Show
     *
     * @param \Foo\Bar\Domain\Model\Booking $booking
     *
     * @return void
     */
    public function showAction($booking)
    {
        $this->view->assign('booking', $booking);
        $view->setConfiguration([
            'booking' => BookingTransformer::class
        ]);
        $this->view->setVariablesToRender(['booking']);
    }
    
}


namespace Foo\Bar\Transformer;

use Foo\Bar\Domain\Model\Booking;

class BookingTransformer
{
   /**
     * @param Booking $booking
     * @return array
     */
    public function transform(Booking $booking)
    {
        return [
            'uid' => $booking->getUid(),
            'start' => $booking->getStart()->format(DateTime::ISO8601),
            'end' => $booking->getEnd()->format(DateTime::ISO8601)
        ];
    }
}