8000 Made all examples of "Quick Tour" complete (all "use", "namespaces", etc.) by javiereguiluz · Pull Request #9527 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Made all examples of "Quick Tour" complete (all "use", "namespaces", etc.) #9527

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
Apr 3, 2018
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
33 changes: 22 additions & 11 deletions quick_tour/flex_recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`_:
Expand All @@ -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;
Expand Down
41 changes: 32 additions & 9 deletions quick_tour/the_architecture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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!" #}
<h1>{{ name|greet }}</h1>

Expand Down
53 changes: 37 additions & 16 deletions quick_tour/the_big_picture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
0