简体   繁体   English

Symfony2路由中的可选参数没有值

[英]Symfony2 Optional parameter in routing has no value

I am struggling with this detail while defining a route in Symfony2 在Symfony2中定义路线时,我在这个细节上苦苦挣扎

Mi routing: 小米路由:

blog:
path: /blog/{page}
defaults: { _controller: ManualRouteBundle:Blog:show, page: 33 }

My controller: 我的控制器:

<?php
namespace Manual\RouteBundle\Controller ;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class BlogController extends Controller{
    public function showAction($page){

        return $this->render('ManualRouteBundle:Blog:show.html.twig') ;
    }
}

My view: 我的观点:

Blog # {{page}}

When I try to access with this address 当我尝试使用该地址访问时

http://test/web/blog 

instead of 代替

http://test/web/blog/1

I get this error 我得到这个错误

 Variable "page" does not exist in ManualRouteBundle:Blog:show.html.twig at line 1
500 Internal Server Error - Twig_Error_Runtime 

Isn't page value supposed to be 33? 页面值不应该是33吗?

I got the answer on #symfony, I have to pass the variable to the view. 我得到了#symfony的答案,我必须将变量传递给视图。

$this->render() like this: $this->render('show.html.twig', array('page' => $page));

Weird behavior imho. 怪异的行为恕我直言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM