Skip to content

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 public

Routes ​

php
#[Route('/posts/{id}', name: 'app_posts_show', methods: ['GET'], requirements: ['id' => '\d+'])]
public function show(int $id): Response { … }
bash
php bin/console debug:router

Controller 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:migrate

Security ​

php
$this->getUser();
#[IsGranted('ROLE_USER')]
$client->loginUser($user); // tests
bash
php bin/console make:user
php bin/console make:auth

Console ​

bash
php bin/console cache:clear
php bin/console make:controller
php bin/console make:command
php bin/console about

Config ​

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 services

PHP tutorial → Symfony ​

PHP tutorialSymfony
mini-router#[Route] attributes
$_POST + validateForm + Validator
CSRF session tokenForm CSRF / token manager
PDODoctrine ORM / DBAL
password_hashPassword hashers
sessionsSecurity firewalls
front controllerpublic/index.php
DIY middlewareEvent subscribers

That's enough to unblock a coding session. For the why, jump back to the chapters.

Best,

-- ł.

Continues after /lab/php-tutorial-for-fe-devs/