Documentation

AbstractController
in package
implements ServiceSubscriberInterface

Provides shortcuts for HTTP-related features in controllers.

Tags
author

Fabien Potencier fabien@symfony.com

Interfaces, Classes and Traits

ServiceSubscriberInterface
A ServiceSubscriber exposes its dependencies via the static {@link getSubscribedServices} method.

Table of Contents

$container  : ContainerInterface
getSubscribedServices()  : array<string|int, string>
Returns an array of service types required by such instances, optionally keyed by the service names used internally.
setContainer()  : ContainerInterface|null
addFlash()  : void
Adds a flash message to the current session for type.
addLink()  : void
Adds a Link HTTP header to the current response.
createAccessDeniedException()  : AccessDeniedException
Returns an AccessDeniedException.
createForm()  : FormInterface
Creates and returns a Form instance from the type of the form.
createFormBuilder()  : FormBuilderInterface
Creates and returns a form builder instance.
createNotFoundException()  : NotFoundHttpException
Returns a NotFoundHttpException.
denyAccessUnlessGranted()  : void
Throws an exception unless the attribute is granted against the current authentication token and optionally supplied subject.
dispatchMessage()  : Envelope
Dispatches a message to the bus.
file()  : BinaryFileResponse
Returns a BinaryFileResponse object with original or customized file name and disposition header.
forward()  : Response
Forwards the request to another controller.
generateUrl()  : string
Generates a URL from the given parameters.
get()  : object
Gets a container service by its id.
getDoctrine()  : ManagerRegistry
Shortcut to return the Doctrine Registry service.
getParameter()  : array<string|int, mixed>|bool|float|int|string|UnitEnum|null
Gets a container parameter by its name.
getUser()  : UserInterface|null
Get a user from the Security Token Storage.
has()  : bool
Returns true if the service id is defined.
isCsrfTokenValid()  : bool
Checks the validity of a CSRF token.
isGranted()  : bool
Checks if the attribute is granted against the current authentication token and optionally supplied subject.
json()  : JsonResponse
Returns a JsonResponse that uses the serializer component if enabled, or json_encode.
redirect()  : RedirectResponse
Returns a RedirectResponse to the given URL.
redirectToRoute()  : RedirectResponse
Returns a RedirectResponse to the given route with the given parameters.
render()  : Response
Renders a view.
renderForm()  : Response
Renders a view and sets the appropriate status code when a form is listed in parameters.
renderView()  : string
Returns a rendered view.
stream()  : StreamedResponse
Streams a view.

Properties

Methods

getSubscribedServices()

Returns an array of service types required by such instances, optionally keyed by the service names used internally.

public static getSubscribedServices() : array<string|int, string>

For mandatory dependencies:

  • ['logger' => 'Psr\Log\LoggerInterface'] means the objects use the "logger" name internally to fetch a service which must implement Psr\Log\LoggerInterface.
  • ['loggers' => 'Psr\Log\LoggerInterface[]'] means the objects use the "loggers" name internally to fetch an iterable of Psr\Log\LoggerInterface instances.
  • ['Psr\Log\LoggerInterface'] is a shortcut for
  • ['Psr\Log\LoggerInterface' => 'Psr\Log\LoggerInterface']

otherwise:

  • ['logger' => '?Psr\Log\LoggerInterface'] denotes an optional dependency
  • ['loggers' => '?Psr\Log\LoggerInterface[]'] denotes an optional iterable dependency
  • ['?Psr\Log\LoggerInterface'] is a shortcut for
  • ['Psr\Log\LoggerInterface' => '?Psr\Log\LoggerInterface']
Return values
array<string|int, string>

The required service types, optionally keyed by service names

addFlash()

Adds a flash message to the current session for type.

protected addFlash(string $type, mixed $message) : void
Parameters
$type : string
$message : mixed
Tags
throws
LogicException
Return values
void

createAccessDeniedException()

Returns an AccessDeniedException.

protected createAccessDeniedException([string $message = 'Access Denied.' ][, Throwable|null $previous = null ]) : AccessDeniedException

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

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

Parameters
$message : string = 'Access Denied.'
$previous : Throwable|null = null
Tags
throws
LogicException

If the Security component is not available

Return values
AccessDeniedException

createForm()

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

protected createForm(string $type[, mixed $data = null ][, array<string|int, mixed> $options = [] ]) : FormInterface
Parameters
$type : string
$data : mixed = null
$options : array<string|int, mixed> = []
Return values
FormInterface

createFormBuilder()

Creates and returns a form builder instance.

protected createFormBuilder([mixed $data = null ][, array<string|int, mixed> $options = [] ]) : FormBuilderInterface
Parameters
$data : mixed = null
$options : array<string|int, mixed> = []
Return values
FormBuilderInterface

createNotFoundException()

Returns a NotFoundHttpException.

protected createNotFoundException([string $message = 'Not Found' ][, Throwable|null $previous = null ]) : NotFoundHttpException

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

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

Parameters
$message : string = 'Not Found'
$previous : Throwable|null = null
Return values
NotFoundHttpException

denyAccessUnlessGranted()

Throws an exception unless the attribute is granted against the current authentication token and optionally supplied subject.

protected denyAccessUnlessGranted(mixed $attribute[, mixed $subject = null ][, string $message = 'Access Denied.' ]) : void
Parameters
$attribute : mixed
$subject : mixed = null
$message : string = 'Access Denied.'
Tags
throws
AccessDeniedException
Return values
void

dispatchMessage()

Dispatches a message to the bus.

protected dispatchMessage(object|Envelope $message[, array<string|int, mixed> $stamps = [] ]) : Envelope
Parameters
$message : object|Envelope

The message or the message pre-wrapped in an envelope

$stamps : array<string|int, mixed> = []
Tags
deprecated

since Symfony 5.4, inject an instance of MessageBusInterface in your controller instead

Return values
Envelope

file()

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

protected file(SplFileInfo|string $file[, string|null $fileName = null ][, string $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT ]) : BinaryFileResponse
Parameters
$file : SplFileInfo|string

File object or path to file to be sent as response

$fileName : string|null = null
$disposition : string = ResponseHeaderBag::DISPOSITION_ATTACHMENT
Return values
BinaryFileResponse

forward()

Forwards the request to another controller.

protected forward(string $controller[, array<string|int, mixed> $path = [] ][, array<string|int, mixed> $query = [] ]) : Response
Parameters
$controller : string

The controller name (a string like "App\Controller\PostController::index" or "App\Controller\PostController" if it is invokable)

$path : array<string|int, mixed> = []
$query : array<string|int, mixed> = []
Return values
Response

generateUrl()

Generates a URL from the given parameters.

protected generateUrl(string $route[, array<string|int, mixed> $parameters = [] ][, int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH ]) : string
Parameters
$route : string
$parameters : array<string|int, mixed> = []
$referenceType : int = UrlGeneratorInterface::ABSOLUTE_PATH
Tags
see
UrlGeneratorInterface
Return values
string

get()

Gets a container service by its id.

protected get(string $id) : object
Parameters
$id : string
Tags
deprecated

since Symfony 5.4, use method or constructor injection in your controller instead

Return values
object

The service

getDoctrine()

Shortcut to return the Doctrine Registry service.

protected getDoctrine() : ManagerRegistry
Tags
throws
LogicException

If DoctrineBundle is not available

deprecated

since Symfony 5.4, inject an instance of ManagerRegistry in your controller instead

Return values
ManagerRegistry

getParameter()

Gets a container parameter by its name.

protected getParameter(string $name) : array<string|int, mixed>|bool|float|int|string|UnitEnum|null
Parameters
$name : string
Return values
array<string|int, mixed>|bool|float|int|string|UnitEnum|null

getUser()

Get a user from the Security Token Storage.

protected getUser() : UserInterface|null
Tags
throws
LogicException

If SecurityBundle is not available

see
TokenInterface::getUser()
Return values
UserInterface|null

has()

Returns true if the service id is defined.

protected has(string $id) : bool
Parameters
$id : string
Tags
deprecated

since Symfony 5.4, use method or constructor injection in your controller instead

Return values
bool

isCsrfTokenValid()

Checks the validity of a CSRF token.

protected isCsrfTokenValid(string $id, string|null $token) : bool
Parameters
$id : string

The id used when generating the token

$token : string|null

The actual token sent with the request that should be validated

Return values
bool

isGranted()

Checks if the attribute is granted against the current authentication token and optionally supplied subject.

protected isGranted(mixed $attribute[, mixed $subject = null ]) : bool
Parameters
$attribute : mixed
$subject : mixed = null
Tags
throws
LogicException
Return values
bool

json()

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

protected json(mixed $data[, int $status = 200 ][, array<string|int, mixed> $headers = [] ][, array<string|int, mixed> $context = [] ]) : JsonResponse
Parameters
$data : mixed
$status : int = 200
$headers : array<string|int, mixed> = []
$context : array<string|int, mixed> = []
Return values
JsonResponse

redirectToRoute()

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

protected redirectToRoute(string $route[, array<string|int, mixed> $parameters = [] ][, int $status = 302 ]) : RedirectResponse
Parameters
$route : string
$parameters : array<string|int, mixed> = []
$status : int = 302
Return values
RedirectResponse

render()

Renders a view.

protected render(string $view[, array<string|int, mixed> $parameters = [] ][, Response|null $response = null ]) : Response
Parameters
$view : string
$parameters : array<string|int, mixed> = []
$response : Response|null = null
Return values
Response

renderForm()

Renders a view and sets the appropriate status code when a form is listed in parameters.

protected renderForm(string $view[, array<string|int, mixed> $parameters = [] ][, Response|null $response = null ]) : Response

If an invalid form is found in the list of parameters, a 422 status code is returned.

Parameters
$view : string
$parameters : array<string|int, mixed> = []
$response : Response|null = null
Return values
Response

renderView()

Returns a rendered view.

protected renderView(string $view[, array<string|int, mixed> $parameters = [] ]) : string
Parameters
$view : string
$parameters : array<string|int, mixed> = []
Return values
string

Search results