abstract class Controller implements ContainerAwareInterface

deprecated since Symfony 4.2, use {@see AbstractController} instead.

Controller is a simple implementation of a Controller.

It provides methods to common features needed in controllers.

Traits

ContainerAware trait.
Common features needed in controllers.

Properties

ContainerInterface $container from ControllerTrait

Methods

setContainer(ContainerInterface $container = null)

No description

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($data, int $status = 200, array $headers = array(), array $context = array())

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

file($file, string $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($attributes, $subject = null)

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

denyAccessUnlessGranted($attributes, $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, $data = null, array $options = array())

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

createFormBuilder($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|null $token)

Checks the validity of a CSRF token.

dispatchMessage(object|Envelope $message)

Dispatches a message to the bus.

addLink(Request $request, Link $link)

Adds a Link HTTP header to the current response.

mixed
getParameter(string $name)

Gets a container configuration parameter by its name.

Details

in ContainerAwareTrait at line 26
setContainer(ContainerInterface $container = null)

Parameters

ContainerInterface $container

in ControllerTrait at line 52
protected bool has(string $id)

Returns true if the service id is defined.

Parameters

string $id

Return Value

bool

in ControllerTrait at line 64
protected object get(string $id)

Gets a container service by its id.

Parameters

string $id

Return Value

object The service

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

Generates a URL from the given parameters.

Parameters

string $route
array $parameters
int $referenceType

Return Value

string

See also

UrlGeneratorInterface

in ControllerTrait at line 88
protected Response forward(string $controller, array $path = array(), array $query = array())

Forwards the request to another controller.

Parameters

string $controller
array $path
array $query

Return Value

Response

in ControllerTrait at line 102
protected RedirectResponse redirect(string $url, int $status = 302)

Returns a RedirectResponse to the given URL.

Parameters

string $url
int $status

Return Value

RedirectResponse

in ControllerTrait at line 112
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
array $parameters
int $status

Return Value

RedirectResponse

in ControllerTrait at line 122
protected JsonResponse json($data, int $status = 200, array $headers = array(), array $context = array())

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

Parameters

$data
int $status
array $headers
array $context

Return Value

JsonResponse

in ControllerTrait at line 142
protected BinaryFileResponse file($file, string $fileName = null, string $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT)

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

Parameters

$file
string $fileName
string $disposition

Return Value

BinaryFileResponse

in ControllerTrait at line 157
protected addFlash(string $type, string $message)

Adds a flash message to the current session for type.

Parameters

string $type
string $message

Exceptions

LogicException

in ControllerTrait at line 173
protected bool isGranted($attributes, $subject = null)

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

Parameters

$attributes
$subject

Return Value

bool

Exceptions

LogicException

in ControllerTrait at line 190
protected denyAccessUnlessGranted($attributes, $subject = null, string $message = 'Access Denied.')

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

Parameters

$attributes
$subject
string $message

Exceptions

AccessDeniedException

in ControllerTrait at line 206
protected string renderView(string $view, array $parameters = array())

Returns a rendered view.

Parameters

string $view
array $parameters

Return Value

string

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

Renders a view.

Parameters

string $view
array $parameters
Response $response

Return Value

Response

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

Streams a view.

Parameters

string $view
array $parameters
StreamedResponse $response

Return Value

StreamedResponse

in ControllerTrait at line 284
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
Exception $previous

Return Value

NotFoundHttpException

in ControllerTrait at line 300
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
Exception $previous

Return Value

AccessDeniedException

Exceptions

LogicException If the Security component is not available

in ControllerTrait at line 314
protected FormInterface createForm(string $type, $data = null, array $options = array())

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

Parameters

string $type
$data
array $options

Return Value

FormInterface

in ControllerTrait at line 324
protected FormBuilderInterface createFormBuilder($data = null, array $options = array())

Creates and returns a form builder instance.

Parameters

$data
array $options

Return Value

FormBuilderInterface

in ControllerTrait at line 336
protected ManagerRegistry getDoctrine()

Shortcut to return the Doctrine Registry service.

Return Value

ManagerRegistry

Exceptions

LogicException If DoctrineBundle is not available

in ControllerTrait at line 356
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()

in ControllerTrait at line 382
protected bool isCsrfTokenValid(string $id, string|null $token)

Checks the validity of a CSRF token.

Parameters

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

Return Value

bool

in ControllerTrait at line 398
protected Envelope dispatchMessage(object|Envelope $message)

Dispatches a message to the bus.

Parameters

object|Envelope $message The message or the message pre-wrapped in an envelope

Return Value

Envelope

Adds a Link HTTP header to the current response.

Parameters

Request $request
Link $link

See also

https://tools.ietf.org/html/rfc5988

at line 40
protected mixed getParameter(string $name)

Gets a container configuration parameter by its name.

Parameters

string $name

Return Value

mixed