douli8040 2016-10-05 13:24
浏览 44
已采纳

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

I would like to provide my controller object like this with hinclude.js :

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

Unfortunately I got this error:

"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).\")

Can you help me ?

Thanks in advance !

  • 写回答

2条回答 默认 最新

  • douan2478 2016-10-05 17:38
    关注

    The exception is right, only scalar/null values are allowed to pass for controller attributes when render_hinclude() is used, because is needed to generate a valid fragment URI from them.


    Why?

    Suppose you try to pass this entity with private properties, when it comes to generate the URI for this parameter happens the following:

    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.


    BTW the statement that you provided is not valid, you should pass this attribute to controller(...) function, something like this:

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

    Then, your controller should have the scalar value id and get the corresponding object. However, use the argument resolver feature to avoid this step:

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

    Next:

    {{ render_hinclude(controller("AppBundle:Default:test", {'object': my_object.id })) }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?