Learn SYMFONY-SECURITY with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
1
Symfony Security Simple API
// config/packages/security.yaml
security:
encoders:
App\Entity\User:
algorithm: auto
providers:
in_memory:
memory:
users:
admin:
password: 'password'
roles: ['ROLE_ADMIN']
firewalls:
main:
anonymous: true
http_basic: ~
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
// src/Controller/TodoController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
class TodoController extends AbstractController {
/**
* @Route("/todos", name="todo_list")
*/
public function list(): JsonResponse {
$todos = ['Task 1', 'Task 2'];
return new JsonResponse($todos);
}
}
Demonstrates a simple Symfony REST API with authentication and role-based access control using security.yaml configuration.