将参数传递给Symfony @ParamConverter

ben*_*888 2 php symfony

我在我的控制器中使用symfony @ParamConverter通过它的slug获取一个实体,但是我没有使用"findBySlug()"方法而是使用更具体的"findWithSomeFeaturesBySlug($ slug)",我遇到了问题因为在findWithSomeFeaturesBySlug($ slug)方法中,我收到$ slug参数作为一个带有'slug'键的关联数组,而不是slug的值.

我的控制器代码如下所示:

/**
* @Route("/some-route/{slug}")
* @ParamConverter("object", class="AcmeBundle:Object", options={"repository_method" = "findWithSomeFeaturesBySlug"})
*/
public function acmeDemoAction(Object $object)
{
    // Controller code here
}
Run Code Online (Sandbox Code Playgroud)

我希望有人可以帮助我.

谢谢.

更新:

对不起,我认为我没有正确解释我的问题.问题是我在"findWithSomeFeaturesBySlug($ slug)"函数中得到了一个关联数组,我需要直接得到$ slug值.

// ObjectRepository
public function findWithSomeFeatures($slug) 
{
    // here I get an associative array in the slug parameter:
    // $slug = array('slug' => 'some_value')
    // And I need $slug = 'some_value'
}
Run Code Online (Sandbox Code Playgroud)

小智 8

我有同样的问题,通过在ParamConverter中添加id选项来修复

/**
* @Route("/some-route/{slug}")
* @ParamConverter("object", class="AcmeBundle:Object", options={"id" = "slug", "repository_method" = "findWithSomeFeaturesBySlug"})
*/
public function acmeDemoAction(Object $object)
{
    // Controller code here
}
Run Code Online (Sandbox Code Playgroud)