我学习了一个symfony2,我会尝试在symfony演示应用程序(博客)中更改路径以启动页面。而不是 FrameworkBundle:模板:模板控制器与静态页面默认/ homepage.html.twig 我想改变路由到 AppBundle:Blog:index 但是我有以下错误:
控制器“AppBundle \ Controller \ BlogController :: indexAction()”要求您为“$ page”参数提供一个值(因为没有默认值,或者因为在此之后存在非可选参数)。
还有方法代码:
/**
* @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));
}
应用程序/配置/ 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%"
我知道,我可以更改参数“$ page = 1”,但我认为它的丑陋修复。有人可以帮帮我吗?
答案 0 :(得分:0)
您可以使用@Override
protected void onStop()
{
unregisterReceiver(sendBroadcastReceiver);
unregisterReceiver(deliveryBroadcastReceiver);
super.onStop();
}
键传递默认参数值:
defaults
答案 1 :(得分:0)
您可以尝试使用此注释
/**
* @Route(
* path = "/page/{page}",
* name = "blog_index",
* defaults = {"page" = 1},
* requirements={"page" : "\d+"}
* )
*/