2017 © Pedro Peláez
 

library controller-util

Utilities for creating Symfony2 responses without dependencies.

image

zenstruck/controller-util

Utilities for creating Symfony2 responses without dependencies.

  • Tuesday, December 15, 2015
  • by kbond
  • Repository
  • 6 Watchers
  • 41 Stars
  • 701 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 2 Open issues
  • 13 Versions
  • 0 % Grown

The README.md

ControllerUtil

Build Status Scrutinizer Code Quality Code Coverage SensioLabs Insight StyleCI Latest Stable Version License, (*1)

When creating Symfony2 controllers as services, you often require the same dependencies for every controller., (*2)

You often need:, (*3)

  • The router for generating redirects.
  • The session for adding flash messages.
  • The templating engine for creating views.
  • The kernel for forwarding to another controller.
  • If using jms/serializer, the serializer.

This library aims to remove those common dependencies by enabling your controllers to return small immutable objects for these tasks. View listeners then take those objects and create the response., (*4)

There is a Symfony2 Bundle and a Silex Service Provider available to ease integration into your project., (*5)

Usage

Forward

To forward the request to another controller, return the Zenstruck\ControllerUtil\Forward object., (*6)

use Zenstruck\ControllerUtil\Forward;

// ...
public function forwardAction()
{
    return new Forward('another.controller:anotherAction', array('foo' => 'bar'));
}
// ...

Arguments:, (*7)

  • $controller: the controller to forward to (required).
  • $parameters: an array of parameters to pass to the controller (default: array()).

Redirect

To redirect to another route, return the Zenstruck\ControllerUtil\Redirect object., (*8)

use Zenstruck\ControllerUtil\Redirect;

// ...
public function redirectAction()
{
    return new Redirect('my_route');

    // with parameters
    return new Redirect('my_route', array('foo' => 'bar'));
}
// ...

Arguments:, (*9)

  • $route: the route to redirect to (required).
  • $parameters: an array of parameters required by the route (default: array()).
  • $statusCode: the status code for the response (default: 302).

FlashRedirect

To redirect to another route and add a flash message, return the Zenstruck\ControllerUtil\FlashRedirect object., (*10)

use Zenstruck\ControllerUtil\FlashRedirect;

// ...
public function redirectAction()
{
    return new FlashRedirect('my_route', array('foo' => 'bar'), array('info' => array('Success!'));

    // factory methods
    return FlashRedirect::create('my_route', array('foo' => 'bar'), 'Error', 'error');
    return FlashRedirect::createSimple('my_route', 'Success');
}
// ...

Arguments:, (*11)

  • $route: the route to redirect to (required).
  • $parameters: an array of parameters required by the route (default: array()).
  • $flashes: an array of flash messages (default: array()).
  • $statusCode: the status code for the response (default: 302).

NOTE: The flashes must be an array in the following format: array($key => array($message)). As this can be cumbersome, there are factory methods., (*12)

Factory Methods:, (*13)

  • FlashRedirect::create:, (*14)

    Arguments:, (*15)

    • $route: the route to redirect to (required).
    • $parameters: an array of parameters required by the route (required).
    • $message: The flash message (required).
    • $type: The flash type (default: info).
    • $statusCode: the status code for the response (default: 302).
  • FlashRedirect::createSimple (for redirects with no route parameters):, (*16)

    Arguments:, (*17)

    • $route: the route to redirect to (required).
    • $message: The flash message (required).
    • $type: The flash type (default: info).
    • $statusCode: the status code for the response (default: 302).

View

To create a view for your response, return the Zenstruck\ControllerUtil\View object. This library has 3 view listeners:, (*18)

  • TemplatingViewListener: for rendering views with the Symfony2 templating component.
  • TwigViewListener: for rendering views with Twig.
  • SerializerViewListener: for rendering non-html views with jms/serializer.
use Zenstruck\ControllerUtil\View;

// ...
public function viewAction()
{
    $object = // ..

    return new View($object, 200);

    // with templates
    return new View($object, 200, 'my_template.html.twig');

    // with an array of fallback templates
    return new View($object, 200, array('my_template.html.twig', 'fallback_template.html.twig'));

    // factory methods
    return View::createCached($object, 86400);
}
// ...

Arguments:, (*19)

  • $data: the data to pass to the view.
  • $statusCode: the status code for the response (default: 200).
  • $template: the template or an array of templates (default: null).
  • $cache: an array of cache options for the response (default: array()).
  • $headers: an array of response headers (default: array()).

Factory Methods:, (*20)

  • View::createCached:, (*21)

    Arguments:, (*22)

    • $data: the data to pass to the view (required).
    • $sharedMaxAge: the shared max age in seconds (required).
    • $statusCode: the status code for the response (default: 200).

NOTES:, (*23)

  • When $template is an array of templates, the view listener will loop through them and render the first one that exists.
  • If no template is provided, you need to have the SerializerViewListener enabled and the request must be non-html. Otherwise an error will result.
  • If $data is not an array, a template is provided, the view listener will convert $data to array('data' => $data) before passing it to your template.

No Content View

This library has a NoContentViewListener which allows your controllers to return an empty View or (if enabled) simply null. The view listener will set a no content response (204)., (*24)

use Zenstruck\ControllerUtil\View;

// ...
public function viewAction()
{
    return new View(null);

    return null; // if enabled
}
// ...

Template

If your views always have a template, you can use the Zenstruck\ControllerUtil\Template object for convenience., (*25)

use Zenstruck\ControllerUtil\Template;

// ...
public function viewAction()
{
    $object = // ..

    return new Template('my_template.html.twig', array('object' => $object));
}
// ...

Arguments:, (*26)

  • $template: the template or an array of templates (required).
  • $parameters: the parameters to pass to the view (default: array()).
  • $statusCode: the status code for the response (default: 200).
  • $cache: an array of cache options for the response (default: array()).
  • $headers: an array of response headers (default: array()).

Factory Methods:, (*27)

  • Template::createCached:, (*28)

    Arguments:, (*29)

    • $template: the template or an array of templates (required).
    • $sharedMaxAge: the shared max age in seconds (required).
    • $parameters: the parameters to pass to the view (default: array()).
    • $statusCode: the status code for the response (default: 200).

Manual installation

It is recommended you use either the Symfony2 Bundle or the Silex Service Provider for including these utilities in your project., (*30)

If you are doing something custom using the Symfony2 Event Dispatcher, you can register the listeners manually:, (*31)

// add the HasFlashesListener
$eventDispatcher->addListener(
    KernelEvents::VIEW,
    array(new HasFlashesListener($flashBag), 'onKernelView'),
    10 // before other events
);

// add the RedirectListener
$eventDispatcher->addListener(
    KernelEvents::VIEW,
    array(new RedirectListener($urlGenerator), 'onKernelView')
);

// add the ForwardListener
$eventDispatcher->addListener(
    KernelEvents::VIEW,
    array(new ForwardListener(), 'onKernelView')
);

// add the TwigViewListener
$eventDispatcher->addListener(
    KernelEvents::VIEW,
    array(new TwigViewListener($twigEnvironment), 'onKernelView')
);

// add the TemplatingViewListener
$eventDispatcher->addListener(
    KernelEvents::VIEW,
    array(new TemplatingViewListener($templating), 'onKernelView')
);

// add the NoContentViewListener
$eventDispatcher->addListener(
    KernelEvents::VIEW,
    array(new NoContentViewListener(true /* false to force an empty view and not allow null */), 'onKernelView'),
    7 // before other events
);

// add the SerializerViewListener
$eventDispatcher->addListener(
    KernelEvents::VIEW,
    array(new SerializerViewListener($serializer), 'onKernelView'),
    5 // before other events
);

NOTES:, (*32)

  • Notice the priority on the HasFlashesListener, NoContentViewListener and SerializerViewListener. These need to be triggered before the other listeners.
  • You should only use either the TemplatingViewListener or TwigViewListener - not both.

The Versions

15/12 2015

dev-master

9999999-dev http://zenstruck.com/projects/ControllerUtil

Utilities for creating Symfony2 responses without dependencies.

  Sources   Download

MIT

The Requires

 

The Development Requires

rest silex symfony2 serializer symfony response controller

15/12 2015

v0.6.0

0.6.0.0 http://zenstruck.com/projects/ControllerUtil

Utilities for creating Symfony2 responses without dependencies.

  Sources   Download

MIT

The Requires

 

The Development Requires

rest silex symfony2 serializer symfony response controller

15/12 2015

dev-update-travis

dev-update-travis http://zenstruck.com/projects/ControllerUtil

Utilities for creating Symfony2 responses without dependencies.

  Sources   Download

MIT

The Requires

 

The Development Requires

rest silex symfony2 serializer symfony response controller

26/10 2015

dev-travis-symfony-3.0

dev-travis-symfony-3.0 http://zenstruck.com/projects/ControllerUtil

Utilities for creating Symfony2 responses without dependencies.

  Sources   Download

MIT

The Requires

 

The Development Requires

rest silex symfony2 serializer symfony response controller

26/10 2015

v0.5.2

0.5.2.0 http://zenstruck.com/projects/ControllerUtil

Utilities for creating Symfony2 responses without dependencies.

  Sources   Download

MIT

The Requires

 

The Development Requires

rest silex symfony2 serializer symfony response controller

07/07 2015

v0.5.1

0.5.1.0 http://zenstruck.com/projects/ControllerUtil

Utilities for creating Symfony2 responses without dependencies.

  Sources   Download

MIT

The Requires

 

rest silex symfony2 serializer symfony response controller

07/07 2015

v0.5.0

0.5.0.0 http://zenstruck.com/projects/ControllerUtil

Utilities for creating Symfony2 responses without dependencies.

  Sources   Download

MIT

The Requires

 

rest silex symfony2 serializer symfony response controller

26/02 2015

dev-safe-messages

dev-safe-messages http://zenstruck.com/projects/ControllerUtil

Utilities for creating Symfony2 responses without dependencies.

  Sources   Download

MIT

The Requires

 

rest silex symfony2 serializer symfony response controller

16/02 2015

v0.4.0

0.4.0.0 http://zenstruck.com/projects/ControllerUtil

Utilities for creating Symfony2 responses without dependencies.

  Sources   Download

MIT

The Requires

 

rest silex symfony2 serializer symfony response controller

17/10 2014

dev-view-array-access

dev-view-array-access http://zenstruck.com/projects/ControllerUtil

Utilities for creating Symfony2 responses without dependencies.

  Sources   Download

MIT

The Requires

 

rest silex symfony2 serializer symfony response controller

15/10 2014

v0.3.0

0.3.0.0 http://zenstruck.com/projects/ControllerUtil

Utilities for creating Symfony2 responses without dependencies.

  Sources   Download

MIT

The Requires

 

rest silex symfony2 serializer symfony response controller

29/03 2014

v0.2.0

0.2.0.0 http://zenstruck.com/projects/ControllerUtil

Utilities for creating Symfony2 responses without dependencies.

  Sources   Download

MIT

The Requires

 

rest silex symfony2 serializer symfony response controller

25/03 2014

v0.1.0

0.1.0.0 http://zenstruck.com/projects/ControllerUtil

Utilities for creating Symfony2 responses without dependencies.

  Sources   Download

MIT

The Requires

 

rest silex symfony2 serializer symfony response controller