Controller Flashcards
What are controllers in general?
- usually methods in a controller-class
- reading a request and returning a response
What can the response contain for example?
HTML page, JSON, XML, a file download, a redirect, a 404 error (or anything else)
Why the AbstractController?
Contains a few http-related helpers like ->createNotFoundException() or ->generateUrl()
Which methods are used to redirect?
- redirectToRoute()
- redirect()
Which method is used to create a Response from a template?
- render()
Which naming convention applies to Controllers?
It should contain the “Controller”-suffix
What happens if Exceptions, which are no instance of HttpException are being thrown?
Symfony will turn it into a 500
How to access the HttpRequest?
Through an argument in the controller-method
What are possibilities to map query-parameters to php values?
- # [MapQueryParameter] (To map separate query parameters to values)
- # [MapQueryString] (To map the all query parameters to an object)
What is the default http response code if a validation for #[MapQueryString] fails?
404
How can the default http response code for failing request-validations be overwritten?
with the argument “validationFailedStatusCode”
How can you return a fallback DTO with #[MapQueryString] in case no query-parameters exist?
As default parameter-value:
#[MapQueryString] UserDto $userDto = new UserDto()
What is the default http response code if a validation for #[MapRequestPayload] fails?
422
Which Response-Type is used to return a file for a download?
BinaryFileResponse
How can you return a file for a download inside a controller?
use Symfony\Component\HttpFoundation\File\File; return $this->file(new File(), 'filename.txt')
What are early hints?
Early hints tell the browser to start downloading some assets even before the application sends the response content (Most of the time css or javascript)
How do you retrieve cookies from a request?
$request->cookies->get('PHPSESSID');
Where does symfony pass the Request-Object?
To any controller that has a parameter typehinted with “Request”
How do you define a Controller-Route in a yaml config?
api_post_edit: path: /api/posts/{id} controller: App\Controller\BlogApiController::edit methods: PUT
How can conditions be checked in route configurations?
post_show: path: /posts/{id} controller: 'App\Controller\DefaultController::showPost' # expressions can retrieve route parameter values using the "params" variable condition: "params['id'] < 1000"
How can you display the flash messages from a session via twig?
{% for message in app.flashes('notice') %} <div class="flash-notice"> {{ message }} </div> {% endfor %}
or, if all of them should be rendered in one place
~~~
{% for label, messages in app.flashes %}
{% for message in messages %}
<div class="flash-{{ label }}">
{{ message }}
</div>
{% endfor %}
{% endfor %}
~~~
How can a flashbag-message be selected without removing it from the stack?
with the method peek();
How can a flashbag-message be selected and automatically be removed from the stack?
with the method get();
How can the default ErrorController be overridden?
By configuration:
# config/packages/framework.yaml framework: error_controller: App\Controller\ErrorController::show