sensio/framework-extra-bundle
Provides annotation-based configuration for Symfony controllers (routing, security, templates, caching, etc.). Note: this bundle is no longer maintained—prefer native PHP attributes in Symfony core (ideally Symfony 6.2+) for full support.
Installation (if not using Symfony 6.2+):
composer require sensio/framework-extra-bundle
Note: For new projects, prefer Symfony’s native attributes (since 6.2).
Enable the Bundle (in config/bundles.php):
return [
// ...
Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
];
First Use Case: Add a route annotation to a controller method:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class ProductController extends AbstractController
{
/**
* @Route("/products/{id}", name="product_show")
*/
public function show($id)
{
return $this->render('product/show.html.twig', ['id' => $id]);
}
}
Key Files:
config/routes.yaml (for YAML-based routes, but annotations are still useful for dynamic logic).src/Controller/ (where annotations are applied).Route Configuration:
/**
* @Route("/api/v1/users", name="user_list", methods={"GET"}, defaults={"page"=1})
*/
public function listUsers(Request $request)
{
// ...
}
{id} captures URL parameters.page=1).GET, POST).ParamConverter (for Doctrine entities):
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
/**
* @ParamConverter("user", class="App\Entity\User")
*/
public function editUser(User $user)
{
// $user is automatically fetched from DB
}
Security Annotations:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
/**
* @Security("is_granted('ROLE_ADMIN')")
*/
public function adminDashboard()
{
// ...
}
Template Rendering:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
/**
* @Template("product/show.html.twig")
*/
public function showAction($id)
{
return ['id' => $id];
}
Integration with Symfony’s Router: Dump routes to verify:
php bin/console debug:router
@Route at the class level for shared prefixes:
/**
* @Route("/api/v1")
*/
class ApiController extends AbstractController
{
/**
* @Route("/users", name="api_user_list")
*/
public function listUsers() { ... }
}
Deprecation Warning:
#[Route("/products/{id}", name: "product_show")]
public function show($id) { ... }
symfony/property-info for partial compatibility.Annotation Parsing Order:
@Route) are merged with method-level ones. Method-level annotations override class-level ones for the same key.Caching Issues:
php bin/console cache:clear
ParamConverter Quirks:
App\Entity\User) exists and is mapped by Doctrine.Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface.Security Annotations:
AccessDeniedException.php bin/console debug:router | grep "product_show"
sensio/framework-extra-bundle's built-in validation (Symfony’s validator component) to catch syntax errors early.Custom ParamConverters:
namespace App\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
class CustomConverter implements ParamConverterInterface
{
public function supports(ParamConverter $configuration)
{
return $configuration->getClass() === 'App\Entity\CustomEntity';
}
public function convert(Request $request, ParamConverter $configuration)
{
// Custom logic
}
}
Register in services.yaml:
services:
App\ParamConverter\CustomConverter:
tags:
- { name: 'sensio_framework_extra.param_converter' }
Event Listeners:
Listen to kernel.request to modify annotations dynamically:
use Symfony\Component\HttpKernel\Event\RequestEvent;
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
if ($request->attributes->get('_route') === 'dynamic_route') {
// Modify annotations via reflection or service container
}
}
Configuration Overrides:
Override bundle settings in config/packages/sensio_framework_extra.yaml:
sensio_framework_extra:
router:
annotations: true # Default; can be disabled
view:
annotations: true
Step-by-Step:
@Route with #[Route].@ParamConverter with #[ParamConverter] (Symfony 6.2+).composer.json to remove sensio/framework-extra-bundle.Tools:
Use symfony/flex or symfony/var-dumper to audit annotations before migration.
How can I help you explore Laravel packages today?