Skip to content

Commit 8adbaab

Browse files
committed
minor #17107 [QuickTour] improve Symfony v6.1 requires PHP v8.1+ (94noni)
This PR was merged into the 6.1 branch. Discussion ---------- [QuickTour] improve Symfony v6.1 requires PHP v8.1+ Those pages are indexed, but found no direct link to them (I do not know why :s) While fixing the required [php v](https://github.com/symfony/symfony-docs/pull/17107/files#diff-329c56dd24f471b82f8f56003889ce976ac7e295a91308891fab064076bdb1d9R17), took chance to improve code example as well <img width="1000" alt="Capture d’écran 2022-08-03 à 21 18 13" src="https://user-images.githubusercontent.com/1358361/182691485-3f381e75-3f21-473c-8a03-153662e2cd0d.png"> Commits ------- 9754f2c [QuickTour] improve Symfony v6.1 requires PHP v8.1+
2 parents cae6c09 + 9754f2c commit 8adbaab

File tree

3 files changed

+29
-43
lines changed

3 files changed

+29
-43
lines changed

quick_tour/flex_recipes.rst

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ are included in your ``composer.json`` file:
2323
2424
"require": {
2525
"...",
26-
"symfony/console": "^4.1",
27-
"symfony/flex": "^1.0",
28-
"symfony/framework-bundle": "^4.1",
29-
"symfony/yaml": "^4.1"
26+
"symfony/console": "^6.1",
27+
"symfony/flex": "^2.0",
28+
"symfony/framework-bundle": "^6.1",
29+
"symfony/yaml": "^6.1"
3030
}
3131
3232
This makes Symfony different from any other PHP framework! Instead of starting with
@@ -86,10 +86,8 @@ Thanks to Flex, after one command, you can start using Twig immediately:
8686
- class DefaultController
8787
+ class DefaultController extends AbstractController
8888
{
89-
/**
90-
* @Route("/hello/{name}")
91-
*/
92-
public function index($name)
89+
#[Route('/hello/{name}', methods: ['GET'])]
90+
public function index(string $name): Response
9391
{
9492
- return new Response("Hello $name!");
9593
+ return $this->render('default/index.html.twig', [
@@ -159,16 +157,15 @@ Are you building an API? You can already return JSON from any controller::
159157
namespace App\Controller;
160158

161159
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
160+
use Symfony\Component\HttpFoundation\JsonResponse;
162161
use Symfony\Component\Routing\Annotation\Route;
163162

164163
class DefaultController extends AbstractController
165164
{
166165
// ...
167166

168-
/**
169-
* @Route("/api/hello/{name}")
170-
*/
171-
public function apiExample($name)
167+
#[Route('/api/hello/{name}', methods: ['GET'])]
168+
public function apiHello(string$name): JsonResponse
172169
{
173170
return $this->json([
174171
'name' => $name,

quick_tour/the_architecture.rst

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ use the logger in a controller, add a new argument type-hinted with ``LoggerInte
2727

2828
use Psr\Log\LoggerInterface;
2929
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
30+
use Symfony\Component\HttpFoundation\Response;
3031
use Symfony\Component\Routing\Annotation\Route;
3132

3233
class DefaultController extends AbstractController
3334
{
34-
/**
35-
* @Route("/hello/{name}")
36-
*/
37-
public function index($name, LoggerInterface $logger)
35+
#[Route('/hello/{name}', methods: ['GET'])]
36+
public function index(string $name, LoggerInterface $logger): Response
3837
{
3938
$logger->info("Saying hello to $name!");
4039

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

9493
class GreetingGenerator
9594
{
96-
public function getRandomGreeting()
95+
public function getRandomGreeting(): string
9796
{
9897
$greetings = ['Hey', 'Yo', 'Aloha'];
9998
$greeting = $greetings[array_rand($greetings)];
@@ -102,7 +101,7 @@ this code directly in your controller, create a new class::
102101
}
103102
}
104103

105-
Great! You can use this immediately in your controller::
104+
Great! You can use it immediately in your controller::
106105

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

116116
class DefaultController extends AbstractController
117117
{
118-
/**
119-
* @Route("/hello/{name}")
120-
*/
121-
public function index($name, LoggerInterface $logger, GreetingGenerator $generator)
118+
#[Route('/hello/{name}', methods: ['GET'])]
119+
public function index(string $name, LoggerInterface $logger, GreetingGenerator $generator): Response
122120
{
123121
$greeting = $generator->getRandomGreeting();
124122

@@ -141,14 +139,11 @@ difference is that it's done in the constructor:
141139
142140
class GreetingGenerator
143141
{
144-
+ private $logger;
145-
+
146-
+ public function __construct(LoggerInterface $logger)
142+
+ public function __construct(private readonly LoggerInterface $logger)
147143
+ {
148-
+ $this->logger = $logger;
149144
+ }
150145
151-
public function getRandomGreeting()
146+
public function getRandomGreeting(): string
152147
{
153148
// ...
154149
@@ -178,11 +173,8 @@ that extends ``AbstractExtension``::
178173

179174
class GreetExtension extends AbstractExtension
180175
{
181-
private $greetingGenerator;
182-
183-
public function __construct(GreetingGenerator $greetingGenerator)
176+
public function __construct(private readonly GreetingGenerator $greetingGenerator)
184177
{
185-
$this->greetingGenerator = $greetingGenerator;
186178
}
187179

188180
public function getFilters()
@@ -192,7 +184,7 @@ that extends ``AbstractExtension``::
192184
];
193185
}
194186

195-
public function greetUser($name)
187+
public function greetUser(string $name): string
196188
{
197189
$greeting = $this->greetingGenerator->getRandomGreeting();
198190

quick_tour/the_big_picture.rst

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ safe & easy!) and offers long-term support.
1414
Downloading Symfony
1515
-------------------
1616

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

1919
Ready? In a terminal, run:
2020

@@ -88,7 +88,7 @@ method inside::
8888

8989
class DefaultController
9090
{
91-
public function index()
91+
public function index(): Response
9292
{
9393
return new Response('Hello!');
9494
}
@@ -125,7 +125,7 @@ like a wildcard that matches anything. And it gets better! Update the controller
125125
class DefaultController
126126
{
127127
- public function index()
128-
+ public function index($name)
128+
+ public function index(string $name): Response
129129
{
130130
- return new Response('Hello!');
131131
+ return new Response("Hello $name!");
@@ -164,10 +164,9 @@ Instead, add the route *right above* the controller method:
164164
165165
class DefaultController
166166
{
167-
+ /**
168-
+ * @Route("/hello/{name}")
169-
+ */
170-
public function index($name) {
167+
+ #[Route('/hello/{name}', methods: ['GET'])]
168+
public function index(string $name): Response
169+
{
171170
// ...
172171
}
173172
}
@@ -187,10 +186,8 @@ in ``DefaultController``::
187186
{
188187
// ...
189188

190-
/**
191-
* @Route("/simplicity")
192-
*/
193-
public function simple()
189+
#[Route('/simplicity', methods: ['GET'])]
190+
public function simple(): Response
194191
{
195192
return new Response('Simple! Easy! Great!');
196193
}

0 commit comments

Comments
 (0)