控制器属性不能包含非标量\/非空值

Controller attributes cannot contain non-scalar\/non-null values

我想像这样为我的控制器对象提供 hinclude.js :

{{ render_hinclude(controller("AppBundle:Default:Test"), { 'object': my_object }) }}

不幸的是我得到了这个错误:

"An exception has been thrown during the rendering of a template (\"Controller attributes cannot contain non-scalar/non-null values (value for key \"my_object\" is not a scalar or null).\")

你能帮帮我吗?

提前致谢!

取决于控制器路由定义,您可能需要传递对象的 ID,所以试试这个:

{{ render_hinclude(controller("AppBundle:Default:Test"), { 'object': my_object.id }) }}

希望对您有所帮助

例外是正确的,当使用 render_hinclude() 时,控制器属性只允许传递 scalar/null 值,因为需要从它们生成有效的片段 URI。


为什么?

假设您尝试传递具有私有属性的实体,当为该参数生成 URI 时会发生以下情况:

var_dump(http_build_query(array('object' => $my_object)))

// output: 
// string(0) ""

http://php.net/manual/en/function.http-build-query.php#refsect1-function.http-build-query-parameters

If query_data is an object, then only public properties will be incorporated into the result.


顺便说一句,您提供的声明无效,您应该将此属性传递给 controller(...) 函数,如下所示:

{{ render_hinclude(controller("AppBundle:Default:test", {'id': my_object.id })) }}

然后,您的控制器应该具有标量值 id 并获取相应的对象。但是,使用参数解析器功能可以避免此步骤:

public function testAction(Object $object) {...}

下一个:

{{ render_hinclude(controller("AppBundle:Default:test", {'object': my_object.id })) }}