Skip to content

Repository files navigation

ViewBundle Bundle

Latest VersionSoftware LicenseBuild StatusCode CoverageQuality ScoreTotal Downloads

The Thruster ViewBundle Bundle. A simple addition to Symfony to provide ability to define views for simple data mappings similar to ElixirPhoenix Views.

Install

Via Composer

$ composer require thruster/view-bundle

Enable Bundle

<?php// app/AppKernel.phppublicfunctionregisterBundles()
{
$bundles = array(
// ...newThruster\Bundle\ViewBundle(),
// ...
);
}

Usage

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

View

<?php// src/AppBundle/View/DefaultView.phpnamespaceAppBundle\View;
useThruster\Bundle\ViewsBundle\View\View;
useAppBundle\Entity\User;
class DefaultView extends View
{
publicfunctionwelcome($name)
{
return [
'msg' => 'Hello, ' . $name
];
}
publicfunctionme(User$user)
{
return [
'name' => $user->getName(),
'email' => $user->getEmail()
];
}
publicfunctionfriend(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;
}
publicfunctionfriends(array$friends)
{
return [
'data' => $this->renderMany([$this, 'friend'], $friends)
];
}
}

Controller

<?phpnamespaceAppBundle\Controller;
useThruster\Bundle\ViewsBundle\Controller\Controller;
class DefaultController extends Controller
{
publicfunctionindexAction()
{
return$this->jsonView('welcome', 'guest');
}
publicfunctionmeAction()
{
return$this->jsonView('AppBundle:Default:me', $this->getUser());
}
publicfunctionfriendAction($id)
{
$friend = $this->getRepository('AppBundle:User')->find($id);
$data = $this->view('AppBundle\View\DefaultView::friend', $friend);
returnnewJsonReponse($data);
}
publicfunctionfriendsAction()
{
$friends = $this->getRepository('AppBundle:User')->findAll();
return$this->jsonView('friends', $friends);
} }

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

License

Please see License File for more information.

About

Thruster View Bundle

Resources

Contributing

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages