Skip to content

Improve routes requirements #1472

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
Feb 5, 2024
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
7 changes: 4 additions & 3 deletions src/Controller/Admin/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Requirement\Requirement;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
use Symfony\Component\Security\Http\Attribute\IsGranted;

Expand Down Expand Up @@ -118,7 +119,7 @@ public function new(
/**
* Finds and displays a Post entity.
*/
#[Route('/{id<\d+>}', name: 'admin_post_show', methods: ['GET'])]
#[Route('/{id}', name: 'admin_post_show', requirements: ['id' => Requirement::POSITIVE_INT], methods: ['GET'])]
public function show(Post $post): Response
{
// This security check can also be performed
Expand All @@ -133,7 +134,7 @@ public function show(Post $post): Response
/**
* Displays a form to edit an existing Post entity.
*/
#[Route('/{id<\d+>}/edit', name: 'admin_post_edit', methods: ['GET', 'POST'])]
#[Route('/{id}/edit', name: 'admin_post_edit', requirements: ['id' => Requirement::POSITIVE_INT], methods: ['GET', 'POST'])]
#[IsGranted('edit', subject: 'post', message: 'Posts can only be edited by their authors.')]
public function edit(Request $request, Post $post, EntityManagerInterface $entityManager): Response
{
Expand All @@ -156,7 +157,7 @@ public function edit(Request $request, Post $post, EntityManagerInterface $entit
/**
* Deletes a Post entity.
*/
#[Route('/{id}/delete', name: 'admin_post_delete', methods: ['POST'])]
#[Route('/{id}/delete', name: 'admin_post_delete', requirements: ['id' => Requirement::POSITIVE_INT], methods: ['POST'])]
#[IsGranted('delete', subject: 'post')]
public function delete(Request $request, Post $post, EntityManagerInterface $entityManager): Response
{
Expand Down
7 changes: 4 additions & 3 deletions src/Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\Cache;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Requirement\Requirement;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
use Symfony\Component\Security\Http\Attribute\IsGranted;

Expand All @@ -46,7 +47,7 @@ final class BlogController extends AbstractController
*/
#[Route('/', name: 'blog_index', defaults: ['page' => '1', '_format' => 'html'], methods: ['GET'])]
#[Route('/rss.xml', name: 'blog_rss', defaults: ['page' => '1', '_format' => 'xml'], methods: ['GET'])]
#[Route('/page/{page<[1-9]\d{0,8}>}', name: 'blog_index_paginated', defaults: ['_format' => 'html'], methods: ['GET'])]
#[Route('/page/{page}', name: 'blog_index_paginated', defaults: ['_format' => 'html'], requirements: ['page' => Requirement::POSITIVE_INT], methods: ['GET'])]
#[Cache(smaxage: 10)]
public function index(Request $request, int $page, string $_format, PostRepository $posts, TagRepository $tags): Response
{
Expand Down Expand Up @@ -74,7 +75,7 @@ public function index(Request $request, int $page, string $_format, PostReposito
*
* See https://symfony.com/doc/current/doctrine.html#automatically-fetching-objects-entityvalueresolver
*/
#[Route('/posts/{slug}', name: 'blog_post', methods: ['GET'])]
#[Route('/posts/{slug}', name: 'blog_post', requirements: ['slug' => Requirement::ASCII_SLUG], methods: ['GET'])]
public function postShow(Post $post): Response
{
// Symfony's 'dump()' function is an improved version of PHP's 'var_dump()' but
Expand All @@ -100,7 +101,7 @@ public function postShow(Post $post): Response
*
* See https://symfony.com/doc/current/doctrine.html#doctrine-entity-value-resolver
*/
#[Route('/comment/{postSlug}/new', name: 'comment_new', methods: ['POST'])]
#[Route('/comment/{postSlug}/new', name: 'comment_new', requirements: ['postSlug' => Requirement::ASCII_SLUG], methods: ['POST'])]
#[IsGranted('IS_AUTHENTICATED')]
public function commentNew(
#[CurrentUser] User $user,
Expand Down