0

I'm working on a project written in Symfony 3 and I have to make a REST API Controller.

I have classic rout for example:

/users : (GET)get all users

/users/{id} : (GET) get a single user

/users : (POST) create a user

and so on..

But I would like to know how to implement a route to search with multiple parameter a user like this URL:

/users?name=John&surname=Doe&age=20&city=London

How can I create this route with query string and search inside it if a value isn't set?

This is a piece of my controller

namespace AppBundle\Controller;

use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\HttpException;
use AppBundle\Entity\User;
use AppBundle\Form\UserType;

class UserController extends FOSRestController
{
    /**
     * @Rest\Get("/users")
     */
    public function getUsersAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $users = $em->getRepository(User::class)->findAll();
        if (!$users) {
            throw new HttpException(400, "Invalid data");
        }
        return $users;

    }

    /**
     * @Rest\Get("/users/{userId}")
     */
    public function getUsersByIdAction($userId, Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $user = $em->getRepository(User::class)->find($userId);

        if (!$userId) {
            throw new HttpException(400, "Invalid id");
        }

        return $user;
    }

    /**
     * @Rest\Post("/users")
     */
    public function postUsersAction(Request $request)
    {
        $user = new User();
        $form = $this->createForm(UserType::class, $user);
        $form->handleRequest($request);
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($user);
            $em->flush();
            return $user;
            //return new JsonResponse( [$data, $status, $headers, $json])
        }

        throw new HttpException(400, "Invalid data");
    }
}

1 Answer 1

2

As usual you have to choices here:

The quick, easy, dirty one:

Using a query parameter you can add individual parameters to your controller:

Example:

/**
 * @Rest\Get("/users/{userId}")
 * @QueryParam(name="foo")
 */
 public function getUsersByIdAction($userId, Request $request, $foo)
 {

Documentation

The slower, safer, cleaner one:

Build a custom form type to process whatever parameters might have been included in your request/query and map them to a proper object which you can then use to extract the parsed values from and pass along to your query builder/ repository / manager.

Documentation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.