【问题标题】:Symfony2 Optional parameter in routing has no valueSymfony2 路由中的可选参数没有值
【发布时间】:2014-11-09 08:08:32
【问题描述】:

在 Symfony2 中定义路线时,我正在努力处理这个细节

小米路由:

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

我的控制器:

<?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') ;
    }
}

我的看法:

Blog # {{page}}

当我尝试使用此地址访问时

http://test/web/blog 

而不是

http://test/web/blog/1

我收到此错误

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

页面值不应该是 33 吗?

【问题讨论】:

    标签: php symfony routing


    【解决方案1】:

    我在#symfony 上得到了答案,我必须将变量传递给视图。

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

    奇怪的行为恕我直言。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 2014-09-17
      • 2013-07-04
      相关资源
      最近更新 更多