trait ControllerTrait

Common features needed in controllers.

Properties

ContainerInterface $container

Methods

bool
has(string $id)

Returns true if the service id is defined.

object
get(string $id)

Gets a container service by its id.

string
generateUrl(string $route, array $parameters = array(), int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)

Generates a URL from the given parameters.

forward(string $controller, array $path = array(), array $query = array())

Forwards the request to another controller.

redirect(string $url, int $status = 302)

Returns a RedirectResponse to the given URL.

redirectToRoute(string $route, array $parameters = array(), int $status = 302)

Returns a RedirectResponse to the given route with the given parameters.

json(mixed $data, int $status = 200, array $headers = array(), array $context = array())

Returns a JsonResponse that uses the serializer component if enabled, or json_encode.

file(SplFileInfo|string $file, string|null $fileName = null, string $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT)

Returns a BinaryFileResponse object with original or customized file name and disposition header.

addFlash(string $type, string $message)

Adds a flash message to the current session for type.

bool
isGranted(mixed $attributes, mixed $subject = null)

Checks if the attributes are granted against the current authentication token and optionally supplied subject.

denyAccessUnlessGranted(mixed $attributes, mixed $subject = null, string $message = 'Access Denied.')

Throws an exception unless the attributes are granted against the current authentication token and optionally supplied subject.

string
renderView(string $view, array $parameters = array())

Returns a rendered view.

render(string $view, array $parameters = array(), Response $response = null)

Renders a view.

stream(string $view, array $parameters = array(), StreamedResponse $response = null)

Streams a view.

createNotFoundException(string $message = 'Not Found', Exception $previous = null)

Returns a NotFoundHttpException.

createAccessDeniedException(string $message = 'Access Denied.', Exception $previous = null)

Returns an AccessDeniedException.

createForm(string $type, mixed $data = null, array $options = array())

Creates and returns a Form instance from the type of the form.

createFormBuilder(mixed $data = null, array $options = array())

Creates and returns a form builder instance.

ManagerRegistry
getDoctrine()

Shortcut to return the Doctrine Registry service.

mixed
getUser()

Get a user from the Security Token Storage.

bool
isCsrfTokenValid(string $id, string $token)

Checks the validity of a CSRF token.

Details

at line 51
protected bool has(string $id)

Returns true if the service id is defined.

Parameters

string $id The service id

Return Value

bool true if the service id is defined, false otherwise

at line 65
protected object get(string $id)

Gets a container service by its id.

Parameters

string $id The service id

Return Value

object The service

at line 83
protected string generateUrl(string $route, array $parameters = array(), int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)

Generates a URL from the given parameters.

Parameters

string $route The name of the route
array $parameters An array of parameters
int $referenceType The type of reference (one of the constants in UrlGeneratorInterface)

Return Value

string The generated URL

See also

UrlGeneratorInterface

at line 99
protected Response forward(string $controller, array $path = array(), array $query = array())

Forwards the request to another controller.

Parameters

string $controller The controller name (a string like BlogBundle:Post:index)
array $path An array of path parameters
array $query An array of query parameters

Return Value

Response A Response instance

at line 119
protected RedirectResponse redirect(string $url, int $status = 302)

Returns a RedirectResponse to the given URL.

Parameters

string $url The URL to redirect to
int $status The status code to use for the Response

Return Value

RedirectResponse

at line 135
protected RedirectResponse redirectToRoute(string $route, array $parameters = array(), int $status = 302)

Returns a RedirectResponse to the given route with the given parameters.

Parameters

string $route The name of the route
array $parameters An array of parameters
int $status The status code to use for the Response

Return Value

RedirectResponse

at line 152
protected JsonResponse json(mixed $data, int $status = 200, array $headers = array(), array $context = array())

Returns a JsonResponse that uses the serializer component if enabled, or json_encode.

Parameters

mixed $data The response data
int $status The status code to use for the Response
array $headers Array of extra headers to add
array $context Context to pass to serializer when using serializer component

Return Value

JsonResponse

at line 176
protected BinaryFileResponse file(SplFileInfo|string $file, string|null $fileName = null, string $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT)

Returns a BinaryFileResponse object with original or customized file name and disposition header.

Parameters

SplFileInfo|string $file File object or path to file to be sent as response
string|null $fileName File name to be sent to response or null (will use original file name)
string $disposition Disposition of response ("attachment" is default, other type is "inline")

Return Value

BinaryFileResponse

at line 194
protected addFlash(string $type, string $message)

Adds a flash message to the current session for type.

Parameters

string $type The type
string $message The message

Exceptions

LogicException

at line 215
protected bool isGranted(mixed $attributes, mixed $subject = null)

Checks if the attributes are granted against the current authentication token and optionally supplied subject.

Parameters

mixed $attributes The attributes
mixed $subject The subject

Return Value

bool

Exceptions

LogicException

at line 236
protected denyAccessUnlessGranted(mixed $attributes, mixed $subject = null, string $message = 'Access Denied.')

Throws an exception unless the attributes are granted against the current authentication token and optionally supplied subject.

Parameters

mixed $attributes The attributes
mixed $subject The subject
string $message The message passed to the exception

Exceptions

AccessDeniedException

at line 257
protected string renderView(string $view, array $parameters = array())

Returns a rendered view.

Parameters

string $view The view name
array $parameters An array of parameters to pass to the view

Return Value

string The rendered view

at line 281
protected Response render(string $view, array $parameters = array(), Response $response = null)

Renders a view.

Parameters

string $view The view name
array $parameters An array of parameters to pass to the view
Response $response A response instance

Return Value

Response A Response instance

at line 311
protected StreamedResponse stream(string $view, array $parameters = array(), StreamedResponse $response = null)

Streams a view.

Parameters

string $view The view name
array $parameters An array of parameters to pass to the view
StreamedResponse $response A response instance

Return Value

StreamedResponse A StreamedResponse instance

at line 352
protected NotFoundHttpException createNotFoundException(string $message = 'Not Found', Exception $previous = null)

Returns a NotFoundHttpException.

This will result in a 404 response code. Usage example:

throw $this->createNotFoundException('Page not found!');

Parameters

string $message A message
Exception $previous The previous exception

Return Value

NotFoundHttpException

at line 373
protected AccessDeniedException createAccessDeniedException(string $message = 'Access Denied.', Exception $previous = null)

Returns an AccessDeniedException.

This will result in a 403 response code. Usage example:

throw $this->createAccessDeniedException('Unable to access this page!');

Parameters

string $message A message
Exception $previous The previous exception

Return Value

AccessDeniedException

Exceptions

LogicException If the Security component is not available

at line 393
protected FormInterface createForm(string $type, mixed $data = null, array $options = array())

Creates and returns a Form instance from the type of the form.

Parameters

string $type The fully qualified class name of the form type
mixed $data The initial data for the form
array $options Options for the form

Return Value

FormInterface

at line 408
protected FormBuilderInterface createFormBuilder(mixed $data = null, array $options = array())

Creates and returns a form builder instance.

Parameters

mixed $data The initial data for the form
array $options Options for the form

Return Value

FormBuilderInterface

at line 422
protected ManagerRegistry getDoctrine()

Shortcut to return the Doctrine Registry service.

Return Value

ManagerRegistry

Exceptions

LogicException If DoctrineBundle is not available

at line 442
protected mixed getUser()

Get a user from the Security Token Storage.

Return Value

mixed

Exceptions

LogicException If SecurityBundle is not available

See also

TokenInterface::getUser()

at line 470
protected bool isCsrfTokenValid(string $id, string $token)

Checks the validity of a CSRF token.

Parameters

string $id The id used when generating the token
string $token The actual token sent with the request that should be validated

Return Value

bool