Reputation:
I'm trying to learn Symfony so i'm very new to it and this is probably a silly question but still, I can't find the answer.
I use the latest version of Symfony (got it today) and the latest WAMP with PHP 7.1.9. I have a Symfony project named "Project" with one bundle : SSTestBundle. In "src/SS/BlogBundle/Resources/config/routing.yml" i have the following route :
ss_blog_home:
path: /{page}
defaults:
_controller: SSBlogBundle:Discussion:index
page: 1
requirements:
page: \d*
I have a controller named DiscussionController, with the following function :
public function indexAction($page)
{
if ($page < 1)
{
throw new NotFoundHttpException('Page -'.$page.'- not existing.');
}
return $this->render('OCPlatformBundle:Advert:index.html.twig');
}
So, if the value of my "page" parameter is invalid, I get a 404 page with the "Page -X- not existing" message. And if the page number is correct, it displays a very basic template to say hello.
If I access my project with "localhost/web/Project/", I get the message : "Page -- not existing". Everything seems to work fine except that $page is not equal to 1 but to nothing !
If I access my project with "localhost/web/Project/5", it displays the page as it should.
If I access my project with "localhost/web/Project/0", it displays the error page as it should, with "Page -0- not existing".
BUT ! If I change my route from '/{page}' to '/test/{page}' and try to access "localhost/web/Project/test/", it works fine and the page is displayed !
Any idea why it returns a null default value for 'page' only if I set my route to the root folder ? T_T
Upvotes: 0
Views: 1440
Reputation: 140
It seems the default parameter needs to be given in the Twig side. e.g.
HTML... {{ path(ss_blog_home, {'page': 1 }) }} HTML
Upvotes: 0
Reputation: 1
By the name of your bundle "OCPlatformBundle" I guess you followed the Openclassroom Symfony tutorial. So I assume that you have a prefix for your routes and hit the same problem as me. At first I didn't understand too but I found that it's indeed a limitation of Symfony when you prefix your routes !
Upvotes: 0
Reputation: 129
Some important conclusions:
Upvotes: 1
Reputation: 59
BUT ! If I change my route from '/{page}' to '/test/{page}' and try to access "localhost/web/Project/test/", it works fine and the page is displayed ! Because of condition Code not looks for type of page so you can send anything you like and it will work
if ($page < 1) { throw new NotFoundHttpException('Page -'.$page.'- not existing.'); }
Upvotes: 0
Reputation: 448
According to the Symfony 3 documentation on routing (see here), you should be able to set up your default route parameter like so:
ss_blog_home:
path: /{page}
defaults: { _controller: SSBlogBundle:Discussion:index, page: 1 } # this sets the default page number for the route to 1.
requirements:
page: '\d+' # this line requires that a digit of length 1 or more is passed in to the route.
Upvotes: 0