2017 © Pedro Peláez
 

library view-bundle

Thruster ViewBundle Bundle

image

thruster/view-bundle

Thruster ViewBundle Bundle

  • Friday, May 4, 2018
  • by gcds
  • Repository
  • 1 Watchers
  • 2 Stars
  • 392 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

ViewBundle Bundle

Latest Version Software License Build Status Code Coverage Quality Score Total Downloads, (*1)

The Thruster ViewBundle Bundle. A simple addition to Symfony to provide ability to define views for simple data mappings similar to Elixir Phoenix Views., (*2)

Install

Via Composer, (*3)

$ composer require thruster/view-bundle

Enable Bundle, (*4)

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Thruster\Bundle\ViewBundle(),
        // ...
    );
}

Usage

Views works similary to Symfony Controllers, all view classes should resident in Bundle/View/ folder. Some real life examples, (*5)

View

<?php
// src/AppBundle/View/DefaultView.php

namespace AppBundle\View;

use Thruster\Bundle\ViewsBundle\View\View;
use AppBundle\Entity\User;

class DefaultView extends View
{
    public function welcome($name)
    {
        return [
            'msg' => 'Hello, ' . $name
        ];
    }

    public function me(User $user)
    {
          return [
            'name' => $user->getName(),
            'email' => $user->getEmail()
          ];
     }

     public function friend(User $friend)
     {
          $friend = $this->renderOne([$this, 'me'], $friend);
          // Also possible just $this->me($friend);

          unset($friend['email']);

          $friend['items'] = $this->renderMany('AppBundle:Item:public_view', $friend->getItems());

          return $friend;
     }

     public function friends(array $friends)
     {
          return [
              'data' => $this->renderMany([$this, 'friend'], $friends)
          ];
     }
}

Controller

<?php

namespace AppBundle\Controller;

use Thruster\Bundle\ViewsBundle\Controller\Controller;

class DefaultController extends Controller
{
    public function indexAction()
    {
        return $this->jsonView('welcome', 'guest');
    }

    public function meAction()
    {
        return $this->jsonView('AppBundle:Default:me', $this->getUser());
    }

    public function friendAction($id)
    {
        $friend = $this->getRepository('AppBundle:User')->find($id);

        $data = $this->view('AppBundle\View\DefaultView::friend', $friend);

        return new JsonReponse($data);
    }

    public function friendsAction()
    {
        $friends = $this->getRepository('AppBundle:User')->findAll();

        return $this->jsonView('friends', $friends);
    }    
}

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details., (*6)

License

Please see License File for more information., (*7)

The Versions

04/05 2018

0.3

0.3.0.0 https://thruster.io

Thruster ViewBundle Bundle

  Sources   Download

MIT

The Requires

 

The Development Requires

thruster view-bundle

04/05 2018

dev-master

9999999-dev https://thruster.io

Thruster ViewBundle Bundle

  Sources   Download

MIT

The Requires

 

The Development Requires

thruster view-bundle

01/02 2018

0.2.0

0.2.0.0 https://thruster.io

Thruster ViewBundle Bundle

  Sources   Download

MIT

The Requires

 

The Development Requires

thruster view-bundle

05/07 2016

0.1

0.1.0.0 https://thruster.io

Thruster ViewBundle Bundle

  Sources   Download

MIT

The Requires

 

The Development Requires

thruster view-bundle