Symfony

Routing & Controllers

Route annotations, parameters, constraints, redirects, Request/Response, JSON responses

20 interview questionsยท
Junior
1

Which PHP attribute should be used to define a route in a Symfony 7 controller?

Answer

The #[Route] attribute from the Symfony\Component\Routing\Attribute namespace is the recommended method for defining routes in Symfony 7. It replaces Doctrine annotations that required special comments. Native PHP attributes provide better IDE integration and compile-time validation.

2

How to define a dynamic route parameter in Symfony?

Answer

Route parameters are defined using curly braces in the route path, for example {id}. This parameter is then automatically injected as an argument to the controller method with the same name. This convention allows automatic binding between the URL and controller parameters.

3

Which Symfony object represents the incoming HTTP request?

Answer

The Request class from the Symfony\Component\HttpFoundation namespace encapsulates all HTTP request data: GET/POST parameters, cookies, headers, uploaded files. It can be automatically injected into controller methods thanks to Symfony's autowiring.

4

How to restrict a route to a specific HTTP method?

5

How to return a JSON response from a Symfony controller?

+17 interview questions

Master Symfony for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free