trait ControllerTrait
Common features needed in controllers.
ContainerInterface | $container |
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. | |
Response | forward(string $controller, array $path = array(), array $query = array()) Forwards the request to another controller. | |
RedirectResponse | redirect(string $url, int $status = 302) Returns a RedirectResponse to the given URL. | |
RedirectResponse | redirectToRoute(string $route, array $parameters = array(), int $status = 302) Returns a RedirectResponse to the given route with the given parameters. | |
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. | |
BinaryFileResponse | 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. | |
Response | render(string $view, array $parameters = array(), Response $response = null) Renders a view. | |
StreamedResponse | stream(string $view, array $parameters = array(), StreamedResponse $response = null) Streams a view. | |
NotFoundHttpException | createNotFoundException(string $message = 'Not Found', Exception $previous = null) Returns a NotFoundHttpException. | |
AccessDeniedException | createAccessDeniedException(string $message = 'Access Denied.', Exception $previous = null) Returns an AccessDeniedException. | |
FormInterface | createForm(string $type, $data = null, array $options = array()) Creates and returns a Form instance from the type of the form. | |
FormBuilderInterface | 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 $message) Dispatches a message to the bus. |
Returns true if the service id is defined.
string | $id |
bool |
Gets a container service by its id.
string | $id |
object | The service |
Generates a URL from the given parameters.
string | $route | |
array | $parameters | |
int | $referenceType |
string |
UrlGeneratorInterface |
Forwards the request to another controller.
string | $controller | |
array | $path | |
array | $query |
Response |
Returns a RedirectResponse to the given URL.
string | $url | |
int | $status |
RedirectResponse |
Returns a RedirectResponse to the given route with the given parameters.
string | $route | |
array | $parameters | |
int | $status |
RedirectResponse |
Returns a JsonResponse that uses the serializer component if enabled, or json_encode.
$data | ||
int | $status | |
array | $headers | |
array | $context |
JsonResponse |
Returns a BinaryFileResponse object with original or customized file name and disposition header.
$file | ||
string | $fileName | |
string | $disposition |
BinaryFileResponse |
Adds a flash message to the current session for type.
string | $type | |
string | $message |
LogicException |
Checks if the attributes are granted against the current authentication token and optionally supplied subject.
$attributes | ||
$subject |
bool |
LogicException |
Throws an exception unless the attributes are granted against the current authentication token and optionally supplied subject.
$attributes | ||
$subject | ||
string | $message |
AccessDeniedException |
Returns a rendered view.
string | $view | |
array | $parameters |
string |
Renders a view.
string | $view | |
array | $parameters | |
Response | $response |
Response |
Streams a view.
string | $view | |
array | $parameters | |
StreamedResponse | $response |
StreamedResponse |
Returns a NotFoundHttpException.
This will result in a 404 response code. Usage example:
throw $this->createNotFoundException('Page not found!');
string | $message | |
Exception | $previous |
NotFoundHttpException |
Returns an AccessDeniedException.
This will result in a 403 response code. Usage example:
throw $this->createAccessDeniedException('Unable to access this page!');
string | $message | |
Exception | $previous |
AccessDeniedException |
LogicException | If the Security component is not available |
Creates and returns a Form instance from the type of the form.
string | $type | |
$data | ||
array | $options |
FormInterface |
Creates and returns a form builder instance.
$data | ||
array | $options |
FormBuilderInterface |
Shortcut to return the Doctrine Registry service.
ManagerRegistry |
LogicException | If DoctrineBundle is not available |
Get a user from the Security Token Storage.
mixed |
LogicException | If SecurityBundle is not available |
TokenInterface::getUser() |
Checks the validity of a CSRF token.
string | $id | The id used when generating the token |
string|null | $token | The actual token sent with the request that should be validated |
bool |
Dispatches a message to the bus.
object | $message | The message to dispatch |
© 2004–2017 Fabien Potencier
Licensed under the MIT License.
https://api.symfony.com/4.1/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.html