0
votes

I learn a symfony2 and i would try to change route to start page in symfony demo application (blog). Instead FrameworkBundle:Template:template controller with static page default/homepage.html.twig i want to change route to AppBundle:Blog:index but i have following error:

Controller "AppBundle\Controller\BlogController::indexAction()" requires that you provide a value for the "$page" argument (because there is no default value or because there is a non optional argument after this one).

And there is a methods code:

 /**
 * @Route("/", name="blog_index", defaults={"page" = 1})
 * @Route("/page/{page}", name="blog_index_paginated", requirements={"page" : "\d+"})
 */
public function indexAction($page)
{
    $query = $this->getDoctrine()->getRepository('AppBundle:Post')->queryLatest();

    $paginator = $this->get('knp_paginator');
    $posts = $paginator->paginate($query, $page, Post::NUM_ITEMS);
    $posts->setUsedRoute('blog_index_paginated');

    return $this->render('blog/index.html.twig', array('posts' => $posts));
}

app/config/routing.yml

app:
    resource: @AppBundle/Controller/
    type:     annotation
    prefix:   /{_locale}
    requirements:
        _locale: %app_locales%
    defaults:
        _locale: %locale%
homepage:
    path: /{_locale}
    requirements:
        _locale: %app_locales%
    defaults:
        _controller: AppBundle:Blog:index
        #_controller: FrameworkBundle:Template:template
        #template:    'default/homepage.html.twig'
        _locale:     "%locale%"

I know, i can change in argument "$page = 1" but i think its ugly fix. Anyone can help me?

2

2 Answers

0
votes

You can pass default parameter values with the defaults key:

homepage:
    path: /{_locale}
    requirements:
        _locale: %app_locales%
    defaults:
        _controller: AppBundle:Blog:index
        _locale: "%locale%"
        page: 1
0
votes

You can try with this annotation

/**
 * @Route(
 *  path = "/page/{page}", 
 *  name = "blog_index",
 *  defaults = {"page" = 1}, 
 *  requirements={"page" : "\d+"}
 * )
 */