dsfd3546 2015-12-13 18:34
浏览 43
已采纳

在@ Route的条件选项中使用请求

I have a controller that renders a fragment of a page; and it may be refreshed using ajax. Until now, this controller uses this at the top:

    if ('/_fragment' !== $request->getPathInfo() && !$request->isXmlHttpRequest()) {
        throw $this->createNotFoundException();
    }

This works well, but I seen that there is a condition option in the @Route annotation, and wanted to use it instead:

/**
 * @Route(
 *     "/widget/render/{cryptedId}",
 *     name = "widget_render",
 *     requirements = {
 *         "cryptedId" = "^[a-zA-Z0-9]+$"
 *     },
 *     condition = "'/_fragment' == request.getPathInfo() or  request.isXmlHttpRequest()"
 * )
 * @Method({"GET"})
 * @Template()
 */

But adding this option gives me the following error:

FatalErrorException in appDevUrlMatcher.php line 319:
-> Error: Call to a member function getPathInfo() on a non-object

That's strange, because from the documentation, context and request are available in the expression parser. By looking at the source code, in UrlMatcher.php, we can see that they should be available:

    if ($route->getCondition() && !$this->getExpressionLanguage()->evaluate($route->getCondition(), array('context' => $this->context, 'request' => $this->request))) {
        return array(self::REQUIREMENT_MISMATCH, null);
    }

I really don't get the point. Any clue?

  • 写回答

1条回答 默认 最新

  • doonbfez815298 2015-12-13 21:12
    关注

    I tried your condition in my Symfony3.0 app and it works perfectly, so I think the problem is somwhere else than in your condition definition.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?