Cheatsheet
Pocket Symfony. Assumes the PHP tutorial cheatsheet already lives in your head.
Create & run
bash
symfony new app --webapp && cd app
symfony serve
# or: php -S 127.0.0.1:8000 -t publicRoutes
php
#[Route('/posts/{id}', name: 'app_posts_show', methods: ['GET'], requirements: ['id' => '\d+'])]
public function show(int $id): Response { … }bash
php bin/console debug:routerController returns
php
return $this->render('posts/index.html.twig', compact('posts'));
return $this->redirectToRoute('app_posts_index');
return $this->json($data, 201);
throw $this->createNotFoundException();Twig
twig
{% extends 'base.html.twig' %}
{% block body %} … {% endblock %}
{{ post.title }}
{{ post.title|raw }}
{{ path('app_posts_show', { id: post.id }) }}
{{ form_start(form) }}{{ form_row(form.title) }}{{ form_end(form) }}
{{ importmap('app') }}Forms & validation
php
$form = $this->createForm(PostType::class, $post);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { … }php
#[Assert\NotBlank]
#[Assert\Length(max: 255)]
private ?string $title = null;Doctrine
php
$em->persist($post);
$em->flush();
$posts->find($id);
$posts->findBy([], ['id' => 'DESC']);
// QueryBuilder join to avoid N+1
->addSelect('a')->join('p.author', 'a')bash
php bin/console make:entity
php bin/console make:migration
php bin/console doctrine:migrations:migrateSecurity
php
$this->getUser();
#[IsGranted('ROLE_USER')]
$client->loginUser($user); // testsbash
php bin/console make:user
php bin/console make:authConsole
bash
php bin/console cache:clear
php bin/console make:controller
php bin/console make:command
php bin/console aboutConfig
ini
APP_ENV=dev
APP_DEBUG=1
DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
MESSENGER_TRANSPORT_DSN=sync://yaml
# prefer %env(...)% in config, inject parameters into servicesPHP tutorial → Symfony
| PHP tutorial | Symfony |
|---|---|
| mini-router | #[Route] attributes |
$_POST + validate | Form + Validator |
| CSRF session token | Form CSRF / token manager |
| PDO | Doctrine ORM / DBAL |
password_hash | Password hashers |
| sessions | Security firewalls |
| front controller | public/index.php |
| DIY middleware | Event subscribers |
That's enough to unblock a coding session. For the why, jump back to the chapters.
Best,
-- ł.