user3262695
user3262695

Reputation:

Symfony 3 - Parameter's default value not returned

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.


SETUP

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.


PROBLEM

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

Answers (5)

Franck Theeten
Franck Theeten

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

paviluf
paviluf

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

Daniella
Daniella

Reputation: 129

Some important conclusions:

  • You made 'page' parameter optional (using in the defaults a default value for 'page'). This means that even if you don't send it in the request, the code will use its default value. So, accesing localhost/web/Project/ should also trigger the controller - and it does.
  • You put in the requirements this regex: '\d*' - this means that the 'page' could be even an empty string or at least a digit. If you want to solve Problem nr 1, you should use '\d+' .
  • Remember that routes can be overidden and the first route defined always win.

Upvotes: 1

Mubasher Iqbal
Mubasher Iqbal

Reputation: 59

  • 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 ! Because you are asking route to have one parameter but you are sending none
  • If I access my project with "localhost/web/Project/5", it displays the page as it should. Because you are sending parameter
  • If I access my project with "localhost/web/Project/5", it displays the page as it should. Because you are sending parameter
  • If I access my project with "localhost/web/Project/0", it displays the error page as it should, with "Page -0- not existing". Because of condition
  • 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

lolsky
lolsky

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

Related Questions