Controllers & Routes

Controllers handle HTTP requests and define your plugin's web interface.

Basic Controller

<?php
namespace Plugins\MyPlugin\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

#[Route(name: 'plugin_my_plugin_')]
class MyController extends AbstractController
{
    #[Route('/', name: 'index', methods: ['GET'])]
    public function index(): Response
    {
        return $this->render('@PluginMyPlugin/index.html.twig', [
            'message' => 'Hello from My Plugin!'
        ]);
    }

    #[Route('/dashboard', name: 'dashboard', methods: ['GET'])]
    public function dashboard(): Response
    {
        // Your logic here
        return $this->render('@PluginMyPlugin/dashboard.html.twig');
    }

    #[Route('/api/data', name: 'api_data', methods: ['GET'])]
    public function apiData(): Response
    {
        $data = ['status' => 'ok', 'message' => 'API response'];
        return $this->json($data);
    }
}

Routing Conventions

Automatic URL prefix: /plugins/{plugin-name}/

  • Route / becomes /plugins/my-plugin/

  • Route /dashboard becomes /plugins/my-plugin/dashboard

  • Route /api/data becomes /plugins/my-plugin/api/data

Route naming: plugin_{plugin_name}_{action}

  • Must start with plugin_

  • Include plugin name in snake_case

  • End with action name

Dependency Injection in Controllers

Form Handling

Security and Permissions

Template Rendering

Use the @Plugin{Name}/ namespace:

Templates are located in plugins/my-plugin/templates/.

JSON Responses

Redirects

Flash Messages

Service Registration

Controllers must be tagged in services.yaml:

Last updated