Today’s Offer Enroll today and get access to premium content.
App Store Google Play
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:

  1. Create a new PHP class in the src/Controller directory.
  2. Use the Symfony\Bundle\FrameworkBundle\Controller\AbstractController as your base class.
  3. 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}