Download the PHP package zenstruck/controller-util without Composer
On this page you can find all versions of the php package zenstruck/controller-util. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zenstruck/controller-util
More information about zenstruck/controller-util
Files in zenstruck/controller-util
Package controller-util
Short Description Utilities for creating Symfony2 responses without dependencies.
License MIT
Homepage http://zenstruck.com/projects/ControllerUtil
Informations about the package controller-util
ControllerUtil
When creating Symfony2 controllers as services, you often require the same dependencies for every controller.
You often need:
- 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.
There is a Symfony2 Bundle and a Silex Service Provider available to ease integration into your project.
Usage
Forward
To forward the request to another controller, return the Zenstruck\ControllerUtil\Forward
object.
Arguments:
$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.
Arguments:
$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.
Arguments:
$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.
Factory Methods:
-
FlashRedirect::create
:Arguments:
$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):Arguments:
$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:
TemplatingViewListener
: for rendering views with the Symfony2 templating component.TwigViewListener
: for rendering views with Twig.SerializerViewListener
: for rendering non-html views with jms/serializer.
Arguments:
$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:
-
View::createCached
:Arguments:
$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:
- 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
toarray('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).
Template
If your views always have a template, you can use the Zenstruck\ControllerUtil\Template
object for convenience.
Arguments:
$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:
-
Template::createCached
:Arguments:
$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.
If you are doing something custom using the Symfony2 Event Dispatcher, you can register the listeners manually:
NOTES:
- Notice the priority on the
HasFlashesListener
,NoContentViewListener
andSerializerViewListener
. These need to be triggered before the other listeners. - You should only use either the
TemplatingViewListener
orTwigViewListener
- not both.