0

I am trying to convert in a route the username to a user. Unfortunately the paramconverter searches always for the id.

I have already tried it with several settings, my current settings look like this:

   /*
    * @ParamConverter("username", class="StregoUserBundle:User")
    * @Rest\View(serializerEnableMaxDepthChecks=true, serializerGroups={"Default","user"})
    * @param User $user username
    */
    public function getUserAction(User $username){
          $return = array('user' => $user);
          return $return;
    }

The route itself is automatically defined by the FOSRestBundle and looks like this:

get_user GET ANY ANY /api/users/{username}.{_format}

What can I do that the user is found via the user name?

m0c
  • 2,180
  • 26
  • 45

2 Answers2

3

From Symfony documentation:

http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#built-in-converters

/*
 * @ParamConverter("username", class="StregoUserBundle:User", options={"id" = "username"})
 * @Rest\View(serializerEnableMaxDepthChecks=true, serializerGroups={"Default","user"})
 * @param User $user username
 */
 public function getUserAction(User $username){
     $return = array('user' => $user);
     return $return;
 }

Set the id to whatever your parameter name is and you're all set.

piotr.jura
  • 810
  • 9
  • 17
0

The Symfony documentation for ParamConverter mentions the mapping hash option as a way to match using multiple fields. However, the comment example that they give uses a single field, which is what you want.

If you want to match an entity using multiple fields use the mapping hash option: the key is route placeholder name and the value is the Doctrine field name:

/**
 * @Route("/blog/{date}/{slug}/comments/{comment_slug}")
 * @ParamConverter("post", options={"mapping": {"date": "date", "slug": "slug"}})
 * @ParamConverter("comment", options={"mapping": {"comment_slug": "slug"}})
 */
public function showAction(Post $post, Comment $comment)
{
}
Peter
  • 808
  • 7
  • 15