Skip to content

Commit 73e55bf

Browse files
committed
Configure routing via attributes.
1 parent 9a002eb commit 73e55bf

File tree

4 files changed

+23
-33
lines changed

4 files changed

+23
-33
lines changed

src/Controller/Admin/BlogController.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
*
3232
* See http://knpbundles.com/keyword/admin
3333
*
34-
* @Route("/admin/post")
3534
* @IsGranted("ROLE_ADMIN")
3635
*
3736
* @author Ryan Weaver <[email protected]>
3837
* @author Javier Eguiluz <[email protected]>
3938
*/
39+
#[Route('/admin/post')]
4040
class BlogController extends AbstractController
4141
{
4242
/**
@@ -49,10 +49,11 @@ class BlogController extends AbstractController
4949
* to create simpler links in the templates. Moreover, in the future we
5050
* could move this annotation to any other controller while maintaining
5151
* the route name and therefore, without breaking any existing link.
52-
*
53-
* @Route("/", methods="GET", name="admin_index")
54-
* @Route("/", methods="GET", name="admin_post_index")
5552
*/
53+
#[
54+
Route("/", methods: ['GET'], name: 'admin_index'),
55+
Route("/", methods: ['GET'], name: 'admin_post_index'),
56+
]
5657
public function index(PostRepository $posts): Response
5758
{
5859
$authorPosts = $posts->findBy(['author' => $this->getUser()], ['publishedAt' => 'DESC']);
@@ -63,12 +64,11 @@ public function index(PostRepository $posts): Response
6364
/**
6465
* Creates a new Post entity.
6566
*
66-
* @Route("/new", methods="GET|POST", name="admin_post_new")
67-
*
6867
* NOTE: the Method annotation is optional, but it's a recommended practice
6968
* to constraint the HTTP methods each controller responds to (by default
7069
* it responds to all methods).
7170
*/
71+
#[Route('/new', methods: ['GET', 'POST'], name: 'admin_post_new')]
7272
public function new(Request $request): Response
7373
{
7474
$post = new Post();
@@ -110,9 +110,8 @@ public function new(Request $request): Response
110110

111111
/**
112112
* Finds and displays a Post entity.
113-
*
114-
* @Route("/{id<\d+>}", methods="GET", name="admin_post_show")
115113
*/
114+
#[Route('/{id<\d+>}', methods: ['GET'], name: 'admin_post_show')]
116115
public function show(Post $post): Response
117116
{
118117
// This security check can also be performed
@@ -127,9 +126,9 @@ public function show(Post $post): Response
127126
/**
128127
* Displays a form to edit an existing Post entity.
129128
*
130-
* @Route("/{id<\d+>}/edit", methods="GET|POST", name="admin_post_edit")
131129
* @IsGranted("edit", subject="post", message="Posts can only be edited by their authors.")
132130
*/
131+
#[Route('/{id<\d+>}/edit', methods: ['GET', 'POST'], name: 'admin_post_edit')]
133132
public function edit(Request $request, Post $post): Response
134133
{
135134
$form = $this->createForm(PostType::class, $post);
@@ -152,9 +151,9 @@ public function edit(Request $request, Post $post): Response
152151
/**
153152
* Deletes a Post entity.
154153
*
155-
* @Route("/{id}/delete", methods="POST", name="admin_post_delete")
156154
* @IsGranted("delete", subject="post")
157155
*/
156+
#[Route('/{id}/delete', methods: ['POST'], name: 'admin_post_delete')]
158157
public function delete(Request $request, Post $post): Response
159158
{
160159
if (!$this->isCsrfTokenValid('delete', $request->request->get('token'))) {

src/Controller/BlogController.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,24 @@
2929
/**
3030
* Controller used to manage blog contents in the public part of the site.
3131
*
32-
* @Route("/blog")
33-
*
3432
* @author Ryan Weaver <[email protected]>
3533
* @author Javier Eguiluz <[email protected]>
3634
*/
35+
#[Route('/blog')]
3736
class BlogController extends AbstractController
3837
{
3938
/**
40-
* @Route("/", defaults={"page": "1", "_format"="html"}, methods="GET", name="blog_index")
41-
* @Route("/rss.xml", defaults={"page": "1", "_format"="xml"}, methods="GET", name="blog_rss")
42-
* @Route("/page/{page<[1-9]\d*>}", defaults={"_format"="html"}, methods="GET", name="blog_index_paginated")
4339
* @Cache(smaxage="10")
4440
*
4541
* NOTE: For standard formats, Symfony will also automatically choose the best
4642
* Content-Type header for the response.
4743
* See https://symfony.com/doc/current/routing.html#special-parameters
4844
*/
45+
#[
46+
Route('/', defaults: ['page' => '1', '_format' => 'html'], methods: ['GET'], name: 'blog_index'),
47+
Route('/rss.xml', defaults: ['page' => '1', '_format' => 'xml'], methods: ['GET'], name: 'blog_rss'),
48+
Route('/page/{page<[1-9]\d*>}', defaults: ['_format' => 'html'], methods: ['GET'], name: 'blog_index_paginated'),
49+
]
4950
public function index(Request $request, int $page, string $_format, PostRepository $posts, TagRepository $tags): Response
5051
{
5152
$tag = null;
@@ -64,13 +65,12 @@ public function index(Request $request, int $page, string $_format, PostReposito
6465
}
6566

6667
/**
67-
* @Route("/posts/{slug}", methods="GET", name="blog_post")
68-
*
6968
* NOTE: The $post controller argument is automatically injected by Symfony
7069
* after performing a database query looking for a Post with the 'slug'
7170
* value given in the route.
7271
* See https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
7372
*/
73+
#[Route('/posts/{slug}', methods: ['GET'], name: 'blog_post')]
7474
public function postShow(Post $post): Response
7575
{
7676
// Symfony's 'dump()' function is an improved version of PHP's 'var_dump()' but
@@ -84,14 +84,14 @@ public function postShow(Post $post): Response
8484
}
8585

8686
/**
87-
* @Route("/comment/{postSlug}/new", methods="POST", name="comment_new")
8887
* @IsGranted("IS_AUTHENTICATED_FULLY")
8988
* @ParamConverter("post", options={"mapping": {"postSlug": "slug"}})
9089
*
9190
* NOTE: The ParamConverter mapping is required because the route parameter
9291
* (postSlug) doesn't match any of the Doctrine entity properties (slug).
9392
* See https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#doctrine-converter
9493
*/
94+
#[Route('/comment/{postSlug}/new', methods: ['POST'], name: 'comment_new')]
9595
public function commentNew(Request $request, Post $post, EventDispatcherInterface $eventDispatcher): Response
9696
{
9797
$comment = new Comment();
@@ -140,9 +140,7 @@ public function commentForm(Post $post): Response
140140
]);
141141
}
142142

143-
/**
144-
* @Route("/search", methods="GET", name="blog_search")
145-
*/
143+
#[Route('/search', methods: ['GET'], name: 'blog_search')]
146144
public function search(Request $request, PostRepository $posts): Response
147145
{
148146
$query = $request->query->get('q', '');

src/Controller/SecurityController.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ class SecurityController extends AbstractController
3030
{
3131
use TargetPathTrait;
3232

33-
/**
34-
* @Route("/login", name="security_login")
35-
*/
33+
#[Route('/login', name: 'security_login')]
3634
public function login(Request $request, Security $security, AuthenticationUtils $helper): Response
3735
{
3836
// if user is already logged in, don't display the login page again
@@ -59,9 +57,8 @@ public function login(Request $request, Security $security, AuthenticationUtils
5957
*
6058
* But, this will never be executed. Symfony will intercept this first
6159
* and handle the logout automatically. See logout in config/packages/security.yaml
62-
*
63-
* @Route("/logout", name="security_logout")
6460
*/
61+
#[Route('/logout', name: 'security_logout')]
6562
public function logout(): void
6663
{
6764
throw new \Exception('This should never be reached!');

src/Controller/UserController.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,14 @@
2323
/**
2424
* Controller used to manage current user.
2525
*
26-
* @Route("/profile")
2726
* @IsGranted("ROLE_USER")
2827
*
2928
* @author Romain Monteil <[email protected]>
3029
*/
30+
#[Route("/profile")]
3131
class UserController extends AbstractController
3232
{
33-
/**
34-
* @Route("/edit", methods="GET|POST", name="user_edit")
35-
*/
33+
#[Route('/edit', methods: ['GET', 'POST'], name: 'user_edit')]
3634
public function edit(Request $request): Response
3735
{
3836
$user = $this->getUser();
@@ -54,9 +52,7 @@ public function edit(Request $request): Response
5452
]);
5553
}
5654

57-
/**
58-
* @Route("/change-password", methods="GET|POST", name="user_change_password")
59-
*/
55+
#[Route("/change-password", methods: ['GET', 'POST'], name: 'user_change_password')]
6056
public function changePassword(Request $request, UserPasswordEncoderInterface $encoder): Response
6157
{
6258
$user = $this->getUser();

0 commit comments

Comments
 (0)