1
votes

Currently following the tutorial of how to create Rest API on Symfony 3.x.

My problem is im stuck with getAction() method using this code.

namespace SwipeBundle\Controller\Backend\API;

use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\View\View;

class UserController extends FOSRestController
{
    /**
     * @Rest\Get("/api/user")
     */
    public function getAction() {
        $restresult = $this->getDoctrine()
            ->getRepository('SwipeBundle:User')
            ->findAll();

        if ($restresult) {
            return new View("there are no users exist", Response::HTTP_NOT_FOUND);
        }
        return $restresult;
    }
}

checking the error on the logs:

Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /api/user"" at /home/swipecom/contactless/var/cache/prod/classes.php line 4595 {"exception":"[object] (Symfony\Component\HttpKernel\Exception\NotFoundHttpException(code: 0): No route found for \"GET /api/user\" at /home/swipecom/contactless/var/cache/prod/classes.php:4595, Symfony\Component\Routing\Exception\ResourceNotFoundException(code: 0): at /home/swipecom/contactless/var/cache/prod/appProdProjectContainerUrlMatcher.php:750)"} []

I checked and configured the FOSRest configuration and Nelmio but still not working.

Here it is:

# Nelmio CORS Configuration
nelmio_cors:
    defaults:
        allow_credentials: false
        allow_origin: ['*']
        allow_headers: ['*']
        allow_methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS']
        max_age: 3600
        hosts: []
        origin_regex: false

# FOSRest Configuration
fos_rest:
    body_listener: true
    format_listener:
        rules:
            - { path: '^/', priorities: ['text/html'], fallback_format: html, prefer_extension: false }
            - { path: '^/api/', priorities: ['json'], fallback_format: json, prefer_extension: false }
    param_fetcher_listener: true
    view:
        view_response_listener: 'force'
        formats:
            json: true
            xml: true
            yml: true
1

1 Answers

1
votes

Use the path attribute as example:

 * @Rest\Get(path="/api/user")

instead of

 * @Rest\Get("/api/user")

You can debug with the console command:

To show all available route:

console debug:router

To see who match your route:

console router:match /api/user

NB:

If the problem persist check with the command

console debug:router --show-controllers

and looking for your controller and see what happen. As example, if your controller is not listed check the routing configuration as described in the doc here.

Hope this help