Skip to content

[Routing] rewrite the routing introduction #19834

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
May 2, 2024
Merged
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
75 changes: 22 additions & 53 deletions quick_tour/the_big_picture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Symfony application:
├─ var/
└─ vendor/

Can we already load the project in a browser? Yes! You can setup
Can we already load the project in a browser? Yes! You can set up
:doc:`Nginx or Apache </setup/web_server_configuration>` and configure their
document root to be the ``public/`` directory. But, for development, it's better
to :doc:`install the Symfony local web server </setup/symfony_server>` and run
Expand All @@ -63,30 +63,18 @@ web app, or a microservice. Symfony starts small, but scales with you.

But before we go too far, let's dig into the fundamentals by building our first page.

Start in ``config/routes.yaml``: this is where *we* can define the URL to our new
page. Uncomment the example that already lives in the file:

.. code-block:: yaml

# config/routes.yaml
index:
path: /
controller: 'App\Controller\DefaultController::index'

This is called a *route*: it defines the URL to your page (``/``) and the "controller":
the *function* that will be called whenever anyone goes to this URL. That function
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;
use Symfony\Component\Routing\Attribute\Route;

class DefaultController
{
#[Route('/', name: 'index')]
public function index(): Response
{
return new Response('Hello!');
Expand All @@ -104,11 +92,21 @@ But the routing system is *much* more powerful. So let's make the route more int

.. code-block:: diff

# config/routes.yaml
index:
- path: /
+ path: /hello/{name}
controller: 'App\Controller\DefaultController::index'
// src/Controller/DefaultController.php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class DefaultController
{
- #[Route('/', name: 'index')]
+ #[Route('/hello/{name}', name: 'index')]
public function index(): Response
{
return new Response('Hello!');
}
}

The URL to this page has changed: it is *now* ``/hello/*``: the ``{name}`` acts
like a wildcard that matches anything. And it gets better! Update the controller too:
Expand All @@ -120,9 +118,11 @@ like a wildcard that matches anything. And it gets better! Update the controller
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class DefaultController
{
#[Route('/hello/{name}', name: 'index')]
- public function index()
+ public function index(string $name): Response
{
Expand All @@ -135,39 +135,8 @@ Try the page out by going to ``http://localhost:8000/hello/Symfony``. You should
see: Hello Symfony! The value of the ``{name}`` in the URL is available as a ``$name``
argument in your controller.

But this can be even simpler! Comment-out the YAML route by adding the
``#`` character:

.. code-block:: yaml

# config/routes.yaml
# index:
# path: /hello/{name}
# controller: 'App\Controller\DefaultController::index'

Instead, add the route *right above* the controller method:

.. code-block:: diff

<?php
// src/Controller/DefaultController.php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
+ use Symfony\Component\Routing\Attribute\Route;

class DefaultController
{
+ #[Route('/hello/{name}', methods: ['GET'])]
public function index(string $name): Response
{
// ...
}
}

This works just like before! But by using attributes, the route and controller
live right next to each other. Need another page? Add another route and method
in ``DefaultController``::
But by using attributes, the route and controller live right next to each
other. Need another page? Add another route and method in ``DefaultController``::

// src/Controller/DefaultController.php
namespace App\Controller;
Expand Down
Loading