我制作了一个带路由的控制器并尝试了一个默认值,即控制器代码:
class luckynumber extends Controller
{
/**
* Matches /lucky/number exactly
*
* @Route("/lucky/number", name="lucky_number")
*/
public function numberAction()
{
$number = rand(1, 100);
return $this->render('lucky/number.html.twig', array('number' => $number));
}
/**
* Matches /lucky/number/{choised} exactly
*
* @Route("/lucky/number/{choised}", name="choised_number", requirements={"choised":"\d+"})
*/
public function undernumberAction(Request $request, $choised = 7)
{
$numberList = array();
for($i = 0; $i < $choised; $i++) {
$numberList[] = rand(1,100);
}
$final_list = implode('-', $numberList);
return $this->render('lucky/choised_number.html.twig', array(
'number' => $choised,
'list' => $final_list
));
}
}
问题是,当我去 http://localhost:8000/lucky/number/ 时,它告诉我:找不到&#34; GET / lucky / number /的路线&#34; 的 这里有什么问题?
答案 0 :(得分:1)
您需要在@Route()
注释选项中添加默认值,而不是在方法参数中。
@Route("/lucky/number/{choised}", name="choised_number", requirements={"choised":"\d+"}, defaults={"choised":7})
有关详细信息,请参阅@Route and @Method文档。
答案 1 :(得分:0)
我找到的唯一方法是来自 twig文件,它是这样的:
<ul>
<li><a href="/lucky/number">lucky number</a></li>
<li><a href="/lucky/number/7">choised lucky number </a></li>
</ul>
答案 2 :(得分:0)
您可以添加2个这样的注释:
/**
* @Route("/blog", defaults={"page"=1})
* @Route("/blog/{$page}", name="blog_list", requirements={"page"="|d+"})
*/
public function list($page)
{
return new Response('<html><body>Welcome to my Blog'.$page.'</body></html>');
}
我正在使用 Symfony 3.4