Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Routing] Improve PHP attributes examples #15515

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
Jul 15, 2021
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
70 changes: 43 additions & 27 deletions 70 routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,13 @@ Use the ``methods`` option to restrict the verbs each route should respond to:
class BlogApiController extends AbstractController
{
#[Route('/api/posts/{id}', methods: ['GET', 'HEAD'])]
public function show(int $id)
public function show(int $id): Response
{
// ... return a JSON response with the post
}

#[Route('/api/posts/{id}', methods: ['PUT'])]
public function edit(int $id)
public function edit(int $id): Response
{
// ... edit a post
}
Expand Down Expand Up @@ -343,6 +343,7 @@ arbitrary matching logic:
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class DefaultController extends AbstractController
Expand All @@ -354,7 +355,7 @@ arbitrary matching logic:
)]
// expressions can also include config parameters:
// condition: "request.headers.get('User-Agent') matches '%app.allowed_browsers%'"
public function contact()
public function contact(): Response
{
// ...
}
Expand Down Expand Up @@ -517,14 +518,15 @@ defined as ``/blog/{slug}``:
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class BlogController extends AbstractController
{
// ...

#[Route('/blog/{slug}', name: 'blog_show')]
public function show(string $slug)
public function show(string $slug): Response
{
// $slug will equal the dynamic part of the URL
// e.g. at /blog/yay-routing, then $slug='yay-routing'
Expand Down Expand Up @@ -623,18 +625,19 @@ the ``{page}`` parameter using the ``requirements`` option:
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class BlogController extends AbstractController
{
#[Route('/blog/{page}', name: 'blog_list', requirements: ['page' => '\d+'])]
public function list(int $page)
public function list(int $page): Response
{
// ...
}

#[Route('/blog/{slug}', name: 'blog_show')]
public function show($slug)
public function show($slug): Response
{
// ...
}
Expand Down Expand Up @@ -750,12 +753,13 @@ concise, but it can decrease route readability when requirements are complex:
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class BlogController extends AbstractController
{
#[Route('/blog/{page<\d+>}', name: 'blog_list')]
public function list(int $page)
public function list(int $page): Response
{
// ...
}
Expand Down Expand Up @@ -804,7 +808,7 @@ visit ``/blog/1``, it will match. But if they visit ``/blog``, it will **not**
match. As soon as you add a parameter to a route, it must have a value.

You can make ``blog_list`` once again match when the user visits ``/blog`` by
adding a default value for the ``{page}`` parameter. When using annotations,
adding a default value for the ``{page}`` parameter. When using annotations or attributes,
default values are defined in the arguments of the controller action. In the
other configuration formats they are defined with the ``defaults`` option:

Expand Down Expand Up @@ -836,12 +840,13 @@ other configuration formats they are defined with the ``defaults`` option:
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class BlogController extends AbstractController
{
#[Route('/blog/{page}', name: 'blog_list', requirements: ['page' => '\d+'])]
public function list(int $page = 1)
public function list(int $page = 1): Response
{
// ...
}
Expand Down Expand Up @@ -940,12 +945,13 @@ parameter:
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class BlogController extends AbstractController
{
#[Route('/blog/{page<\d+>?1}', name: 'blog_list')]
public function list(int $page)
public function list(int $page): Response
{
// ...
}
Expand Down Expand Up @@ -1194,7 +1200,7 @@ and in route imports. Symfony defines some special attributes with the same name
'_format' => 'html|xml',
],
)]
public function search()
public function search(): Response
{
}
}
Expand Down Expand Up @@ -1285,12 +1291,14 @@ the controllers of the routes:
// src/Controller/BlogController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class BlogController
class BlogController extends AbstractController
{
#[Route('/blog/{page}', name: 'blog_index', defaults: ['page' => 1, 'title' => 'Hello world!'])]
public function index(int $page, string $title)
public function index(int $page, string $title): Response
{
// ...
}
Expand Down Expand Up @@ -1375,13 +1383,15 @@ A possible solution is to change the parameter requirements to be more permissiv

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


use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class DefaultController
class DefaultController extends AbstractController
{
#[Route('/share/{token}', name: 'share', requirements: ['token' => '.+'])]
public function share($token)
public function share($token): Response
{
// ...
}
Expand Down Expand Up @@ -1493,20 +1503,22 @@ when importing the routes.

// src/Controller/BlogController.php
namespace App\Controller;


use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/blog', requirements: ['_locale' => 'en|es|fr'], name: 'blog_')]
class BlogController
class BlogController extends AbstractController
{
#[Route('/{_locale}', name: 'index')]
public function index()
public function index(): Response
{
// ...
}

#[Route('/{_locale}/posts/{slug}', name: 'show')]
public function show(Post $post)
public function show(Post $post): Response
{
// ...
}
Expand Down Expand Up @@ -1837,18 +1849,19 @@ host name:
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class MainController extends AbstractController
{
#[Route('/', name: 'mobile_homepage', host: 'm.example.com')]
public function mobileHomepage()
public function mobileHomepage(): Response
{
// ...
}

#[Route('/', name: 'homepage')]
public function homepage()
public function homepage(): Response
{
// ...
}
Expand Down Expand Up @@ -1946,6 +1959,7 @@ multi-tenant applications) and these parameters can be validated too with
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class MainController extends AbstractController
Expand All @@ -1957,13 +1971,13 @@ multi-tenant applications) and these parameters can be validated too with
defaults: ['subdomain' => 'm'],
requirements: ['subdomain' => 'm|mobile'],
)]
public function mobileHomepage()
public function mobileHomepage(): Response
{
// ...
}

#[Route('/', name: 'homepage')]
public function homepage()
public function homepage(): Response
{
// ...
}
Expand Down Expand Up @@ -2101,6 +2115,7 @@ avoids the need for duplicating routes, which also reduces the potential bugs:
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class CompanyController extends AbstractController
Expand All @@ -2109,7 +2124,7 @@ avoids the need for duplicating routes, which also reduces the potential bugs:
'en' => '/about-us',
'nl' => '/over-ons'
], name: 'about_us')]
public function about()
public function about(): Response
{
// ...
}
Expand Down Expand Up @@ -2257,7 +2272,7 @@ locale.
$routes->import('../../src/Controller/', 'annotation')
->host([
'en' => 'https://www.example.com',
'nl' => 'https://www.example.nl'
'nl' => 'https://www.example.nl',
])
;
};
Expand Down Expand Up @@ -2711,12 +2726,13 @@ each route explicitly:
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class SecurityController extends AbstractController
{
#[Route('/login', name: 'login', schemes: ['https'])]
public function login()
public function login(): Response
{
// ...
}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.