Skip to content

Commit 0f4b86a

Browse files
committed
bug #304 Improve routes and fix bug with pagination (voronkovich)
This PR was merged into the master branch. Discussion ---------- Improve routes and fix bug with pagination For now the paginator returns an empty array when requested page is out of range. I think we should handle this situation manually and throw the 404 exception. See KnpLabs/knp-components#78 Also, route definition accepts 0 as page parameter and paginator throws an exception. Commits ------- 9f699de Improve routes and fix bug with pagination
2 parents 0091056 + 9f699de commit 0f4b86a

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/AppBundle/Controller/Admin/BlogController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function newAction(Request $request)
111111
/**
112112
* Finds and displays a Post entity.
113113
*
114-
* @Route("/{id}", requirements={"id" = "\d+"}, name="admin_post_show")
114+
* @Route("/{id}", requirements={"id": "\d+"}, name="admin_post_show")
115115
* @Method("GET")
116116
*/
117117
public function showAction(Post $post)
@@ -134,7 +134,7 @@ public function showAction(Post $post)
134134
/**
135135
* Displays a form to edit an existing Post entity.
136136
*
137-
* @Route("/{id}/edit", requirements={"id" = "\d+"}, name="admin_post_edit")
137+
* @Route("/{id}/edit", requirements={"id": "\d+"}, name="admin_post_edit")
138138
* @Method({"GET", "POST"})
139139
*/
140140
public function editAction(Post $post, Request $request)

src/AppBundle/Controller/BlogController.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333
class BlogController extends Controller
3434
{
3535
/**
36-
* @Route("/", name="blog_index", defaults={"page" = 1})
37-
* @Route("/page/{page}", name="blog_index_paginated", requirements={"page" : "\d+"})
36+
* @Route("/", defaults={"page": 1}, name="blog_index")
37+
* @Route("/page/{page}", requirements={"page": "[1-9]\d*"}, name="blog_index_paginated")
38+
* @Method("GET")
3839
* @Cache(smaxage="10")
3940
*/
4041
public function indexAction($page)
@@ -45,11 +46,16 @@ public function indexAction($page)
4546
$posts = $paginator->paginate($query, $page, Post::NUM_ITEMS);
4647
$posts->setUsedRoute('blog_index_paginated');
4748

49+
if (0 === count($posts)) {
50+
throw $this->createNotFoundException();
51+
}
52+
4853
return $this->render('blog/index.html.twig', array('posts' => $posts));
4954
}
5055

5156
/**
5257
* @Route("/posts/{slug}", name="blog_post")
58+
* @Method("GET")
5359
*
5460
* NOTE: The $post controller argument is automatically injected by Symfony
5561
* after performing a database query looking for a Post with the 'slug'
@@ -62,10 +68,9 @@ public function postShowAction(Post $post)
6268
}
6369

6470
/**
65-
* @Route("/comment/{postSlug}/new", name = "comment_new")
66-
* @Security("is_granted('IS_AUTHENTICATED_FULLY')")
67-
*
71+
* @Route("/comment/{postSlug}/new", name="comment_new")
6872
* @Method("POST")
73+
* @Security("is_granted('IS_AUTHENTICATED_FULLY')")
6974
* @ParamConverter("post", options={"mapping": {"postSlug": "slug"}})
7075
*
7176
* NOTE: The ParamConverter mapping is required because the route parameter

src/AppBundle/Controller/SecurityController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace AppBundle\Controller;
1313

1414
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
1516
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
1617

1718
/**
@@ -25,6 +26,7 @@ class SecurityController extends Controller
2526
{
2627
/**
2728
* @Route("/login", name="security_login_form")
29+
* @Method("GET")
2830
*/
2931
public function loginAction()
3032
{

0 commit comments

Comments
 (0)