Skip to content

[QuickTour] improve Symfony v6.1 requires PHP v8.1+ #17107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions quick_tour/flex_recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ are included in your ``composer.json`` file:

"require": {
"...",
"symfony/console": "^4.1",
"symfony/flex": "^1.0",
"symfony/framework-bundle": "^4.1",
"symfony/yaml": "^4.1"
"symfony/console": "^6.1",
"symfony/flex": "^2.0",
"symfony/framework-bundle": "^6.1",
"symfony/yaml": "^6.1"
}

This makes Symfony different from any other PHP framework! Instead of starting with
Expand Down Expand Up @@ -86,10 +86,8 @@ Thanks to Flex, after one command, you can start using Twig immediately:
- class DefaultController
+ class DefaultController extends AbstractController
{
/**
* @Route("/hello/{name}")
*/
public function index($name)
#[Route('/hello/{name}', methods: ['GET'])]
public function index(string $name): Response
{
- return new Response("Hello $name!");
+ return $this->render('default/index.html.twig', [
Expand Down Expand Up @@ -159,16 +157,15 @@ Are you building an API? You can already return JSON from any controller::
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;

class DefaultController extends AbstractController
{
// ...

/**
* @Route("/api/hello/{name}")
*/
public function apiExample($name)
#[Route('/api/hello/{name}', methods: ['GET'])]
public function apiHello(string$name): JsonResponse
{
return $this->json([
'name' => $name,
Expand Down
32 changes: 12 additions & 20 deletions quick_tour/the_architecture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ use the logger in a controller, add a new argument type-hinted with ``LoggerInte

use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class DefaultController extends AbstractController
{
/**
* @Route("/hello/{name}")
*/
public function index($name, LoggerInterface $logger)
#[Route('/hello/{name}', methods: ['GET'])]
public function index(string $name, LoggerInterface $logger): Response
{
$logger->info("Saying hello to $name!");

Expand Down Expand Up @@ -93,7 +92,7 @@ this code directly in your controller, create a new class::

class GreetingGenerator
{
public function getRandomGreeting()
public function getRandomGreeting(): string
{
$greetings = ['Hey', 'Yo', 'Aloha'];
$greeting = $greetings[array_rand($greetings)];
Expand All @@ -102,7 +101,7 @@ this code directly in your controller, create a new class::
}
}

Great! You can use this immediately in your controller::
Great! You can use it immediately in your controller::

<?php
// src/Controller/DefaultController.php
Expand All @@ -111,14 +110,13 @@ Great! You can use this immediately in your controller::
use App\GreetingGenerator;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class DefaultController extends AbstractController
{
/**
* @Route("/hello/{name}")
*/
public function index($name, LoggerInterface $logger, GreetingGenerator $generator)
#[Route('/hello/{name}', methods: ['GET'])]
public function index(string $name, LoggerInterface $logger, GreetingGenerator $generator): Response
{
$greeting = $generator->getRandomGreeting();

Expand All @@ -141,14 +139,11 @@ difference is that it's done in the constructor:

class GreetingGenerator
{
+ private $logger;
+
+ public function __construct(LoggerInterface $logger)
+ public function __construct(private readonly LoggerInterface $logger)
+ {
+ $this->logger = $logger;
+ }

public function getRandomGreeting()
public function getRandomGreeting(): string
{
// ...

Expand Down Expand Up @@ -178,11 +173,8 @@ that extends ``AbstractExtension``::

class GreetExtension extends AbstractExtension
{
private $greetingGenerator;

public function __construct(GreetingGenerator $greetingGenerator)
public function __construct(private readonly GreetingGenerator $greetingGenerator)
{
$this->greetingGenerator = $greetingGenerator;
}

public function getFilters()
Expand All @@ -192,7 +184,7 @@ that extends ``AbstractExtension``::
];
}

public function greetUser($name)
public function greetUser(string $name): string
{
$greeting = $this->greetingGenerator->getRandomGreeting();

Expand Down
19 changes: 8 additions & 11 deletions quick_tour/the_big_picture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ safe & easy!) and offers long-term support.
Downloading Symfony
-------------------

First, make sure you've installed `Composer`_ and have PHP 8.0.2 or higher.
First, make sure you've installed `Composer`_ and have PHP 8.1 or higher.

Ready? In a terminal, run:

Expand Down Expand Up @@ -88,7 +88,7 @@ method inside::

class DefaultController
{
public function index()
public function index(): Response
{
return new Response('Hello!');
}
Expand Down Expand Up @@ -125,7 +125,7 @@ like a wildcard that matches anything. And it gets better! Update the controller
class DefaultController
{
- public function index()
+ public function index($name)
+ public function index(string $name): Response
{
- return new Response('Hello!');
+ return new Response("Hello $name!");
Expand Down Expand Up @@ -164,10 +164,9 @@ Instead, add the route *right above* the controller method:

class DefaultController
{
+ /**
+ * @Route("/hello/{name}")
+ */
public function index($name) {
+ #[Route('/hello/{name}', methods: ['GET'])]
public function index(string $name): Response
{
// ...
}
}
Expand All @@ -187,10 +186,8 @@ in ``DefaultController``::
{
// ...

/**
* @Route("/simplicity")
*/
public function simple()
#[Route('/simplicity', methods: ['GET'])]
public function simple(): Response
{
return new Response('Simple! Easy! Great!');
}
Expand Down