Intermediate
How do you create a Symfony controller?
A Symfony controller is a PHP function that responds to user requests. It is responsible for fetching data from the model and passing it to the view.
Follow these steps to create a controller:
- Create a new PHP class in the
src/Controller
directory. - Use the
Symfony\Bundle\FrameworkBundle\Controller\AbstractController
as your base class. - Define a method in the class that will handle the request.
Here’s a simple example:
namespace App\Controller;\nuse Symfony\Component\HttpFoundation\Response;\nclass DefaultController extends AbstractController {\n public function index() {\n return new Response('Hello, Symfony!');\n }\n}