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!" #}