From 7ab953c29108f9479d2f1b9ff0359e4806d5df4d Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 30 Mar 2018 18:00:58 +0200 Subject: [PATCH] Made all examples of "Quick Tour" complete (all "use", "namespaces", etc.) --- quick_tour/flex_recipes.rst | 33 +++++++++++++------- quick_tour/the_architecture.rst | 41 +++++++++++++++++++------ quick_tour/the_big_picture.rst | 53 +++++++++++++++++++++++---------- 3 files changed, 91 insertions(+), 36 deletions(-) diff --git a/quick_tour/flex_recipes.rst b/quick_tour/flex_recipes.rst index 692c62faa9d..e6a6a4e3eaa 100644 --- a/quick_tour/flex_recipes.rst +++ b/quick_tour/flex_recipes.rst @@ -76,8 +76,10 @@ Thanks to Flex, after one command, you can start using Twig immediately: .. code-block:: diff // src/Controller/DefaultController.php - // ... + namespace App\Controller; + use Symfony\Component\Routing\Annotation\Route; + - use Symfony\Component\HttpFoundation\Response; + use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -class DefaultController @@ -150,15 +152,26 @@ Rich API Support Are you building an API? You can already return JSON easily from any controller:: - /** - * @Route("/api/hello/{name}") - */ - public function apiExample($name) + // src/Controller/DefaultController.php + namespace App\Controller; + + use Symfony\Component\Routing\Annotation\Route; + use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; + + class DefaultController extends AbstractController { - return $this->json([ - 'name' => $name, - 'symfony' => 'rocks', - ]); + // ... + + /** + * @Route("/api/hello/{name}") + */ + public function apiExample($name) + { + return $this->json([ + 'name' => $name, + 'symfony' => 'rocks', + ]); + } } But for a *truly* rich API, try installing `Api Platform`_: @@ -176,8 +189,6 @@ rich API for a ``product`` table? Create a ``Product`` entity and give it the ``@ApiResource()`` annotation:: // src/Entity/Product.php - // ... - namespace App\Entity; use ApiPlatform\Core\Annotation\ApiResource; diff --git a/quick_tour/the_architecture.rst b/quick_tour/the_architecture.rst index 9dc32fc6c1d..6cd28fdb278 100644 --- a/quick_tour/the_architecture.rst +++ b/quick_tour/the_architecture.rst @@ -21,14 +21,24 @@ Want a logging system? No problem: This installs and configures (via a recipe) the powerful `Monolog`_ library. To use the logger in a controller, add a new argument type-hinted with ``LoggerInterface``:: + // src/Controller/DefaultController.php + namespace App\Controller; + use Psr\Log\LoggerInterface; - // ... + use Symfony\Component\Routing\Annotation\Route; + use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; - public function index($name, LoggerInterface $logger) + class DefaultController extends AbstractController { - $logger->info("Saying hello to $name!"); + /** + * @Route("/hello/{name}") + */ + public function index($name, LoggerInterface $logger) + { + $logger->info("Saying hello to $name!"); - // ... + // ... + } } That's it! The new log message will be written to ``var/log/dev.log``. Of course, this @@ -89,16 +99,27 @@ this code directly in your controller, create a new class:: Great! You can use this immediately in your controller:: + // src/Controller/DefaultController.php + namespace App\Controller; + use App\GreetingGenerator; - // ... + use Psr\Log\LoggerInterface; + use Symfony\Component\Routing\Annotation\Route; + use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; - public function index($name, LoggerInterface $logger, GreetingGenerator $generator) + class DefaultController extends AbstractController { - $greeting = $generator->getRandomGreeting(); + /** + * @Route("/hello/{name}") + */ + public function index($name, LoggerInterface $logger, GreetingGenerator $generator) + { + $greeting = $generator->getRandomGreeting(); - $logger->info("Saying $greeting to $name!"); + $logger->info("Saying $greeting to $name!"); - // ... + // ... + } } That's it! Symfony will instantiate the ``GreetingGenerator`` automatically and @@ -108,6 +129,7 @@ difference is that it's done in the constructor: .. code-block:: diff + // src/GreetingGenerator.php + use Psr\Log\LoggerInterface; class GreetingGenerator @@ -174,6 +196,7 @@ After creating just *one* file, you can use this immediately: .. code-block:: twig + {# templates/default/index.html.twig #} {# Will print something like "Hey Symfony!" #}

{{ name|greet }}

diff --git a/quick_tour/the_big_picture.rst b/quick_tour/the_big_picture.rst index eaf2edb3fb3..129238125e8 100644 --- a/quick_tour/the_big_picture.rst +++ b/quick_tour/the_big_picture.rst @@ -69,6 +69,7 @@ page. Uncomment the example that already lives in the file: .. code-block:: yaml + # config/routes.yaml index: path: / controller: 'App\Controller\DefaultController::index' @@ -80,6 +81,7 @@ doesn't exist yet, so let's create it! In ``src/Controller``, create a new ``DefaultController`` class and an ``index`` method inside:: + // src/Controller/DefaultController.php namespace App\Controller; use Symfony\Component\HttpFoundation\Response; @@ -115,13 +117,18 @@ like a wildcard that matches anything. And it gets better! Update the controller .. code-block:: diff // src/Controller/DefaultController.php - // ... + namespace App\Controller; + + use Symfony\Component\HttpFoundation\Response; - - public function index() - + public function index($name) + class DefaultController { - - return new Response('Hello!'); - + return new Response("Hello $name!"); + - public function index() + + public function index($name) + { + - return new Response('Hello!'); + + return new Response("Hello $name!"); + } } Try the page out by going to ``http://localhost:8000/hello/Symfony``. You should @@ -148,28 +155,42 @@ Instead, add the route *right above* the controller method: .. code-block:: diff // src/Controller/DefaultController.php - // ... + namespace App\Controller; + use Symfony\Component\HttpFoundation\Response; + use Symfony\Component\Routing\Annotation\Route; - + /** - + * @Route("/hello/{name}") - + */ - public function index($name) + class DefaultController + { + + /** + + * @Route("/hello/{name}") + + */ + public function index($name) { + // ... + } + } This works just like before! But by using annotations, the route and controller live right next to each other. Need another page? Just add another route and method in ``DefaultController``:: // src/Controller/DefaultController.php - // ... + namespace App\Controller; + + use Symfony\Component\HttpFoundation\Response; + use Symfony\Component\Routing\Annotation\Route; - /** - * @Route("/simplicity") - */ - public function simple() + class DefaultController { - return new Response('Simple! Easy! Great!'); + // ... + + /** + * @Route("/simplicity") + */ + public function simple() + { + return new Response('Simple! Easy! Great!'); + } } Routing can do *even* more, but we'll save that for another time! Right now, our