0
votes

I get an error message, that no route is found for "GET/users":

Level: Error, Channel: request, Message: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /users"" at /Users/work/project/vendor/symfony/http-kernel/EventListener/RouterListener.php line 139

But I actually set a route for "users", so I do not know how to solve this:

<?php

namespace App\Controller;

use DataTables\DataTablesInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;

/**
*
* @Route("/users", name="users")
*
* @param Request $request
* @param DataTablesInterface $datatables
* @return JsonResponse
*/

class DataTableController extends Controller
{

  const ID = 'users';

  public function usersAction(Request $request, DataTablesInterface $datatables): JsonResponse
  {
    try {
      // Tell the DataTables service to process the request,
      // specifying ID of the required handler.
      $results = $datatables->handle($request, 'users');

      return $this->json($results);
    }
    catch (HttpException $e) {
      // In fact the line below returns 400 HTTP status code.
      // The message contains the error description.
      return $this->json($e->getMessage(), $e->getStatusCode());
    }
  }

}
1
Moving your annotations from the class-level to the function-level of usersAction() should do it. Ensure your cache is also up-to-date if you do not use the dev env.NaeiKinDus
@NaeiKinDus Thank you very much! I moved it to the function level as you recommended: i.imgur.com/mDnNMo0.png But still the same error messagepeace_love
if dbrumann's answer didn't help please paste your config & config/services.ymlNaeiKinDus
@NaeiKinDus This is the service.yaml: i.imgur.com/MzdS7FT.pngpeace_love

1 Answers

5
votes

The annotation on top of the class only defines a prefix, not the actual route. The reason why becomes clear when you have multiple methods in the class. How can the router identify which one to call?

In other words you either have to move the annotation to the method or add another one like this to the action:

<?php declare(strict_types = 1);

namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/users", name="users_")
 */
class UserController
{
    /**
     * @Route("", name="list")
     */
    public function index()
    {
        return new Response('TODO: Add user list');
    }
}

This will append the route definition from the action to the one on the class, like one would expect from a prefixed route.

If you want to know which routes Symfony recognizes you can use the debug command in the console:

php bin/console debug:router

or for a specific route:

php bin/console debug:router users

This is the output I get in my demo application:

php bin/console debug:router

------------ -------- -------- ------ --------
Name         Method   Scheme   Host   Path
------------ -------- -------- ------ --------
users_list   ANY      ANY      ANY    /users
------------ -------- -------- ------ --------

php bin/console debug:router users_list
+--------------+---------------------------------------------------------+
| Property     | Value                                                   |
+--------------+---------------------------------------------------------+
| Route Name   | users_list                                              |
| Path         | /users                                                  |
| Path Regex   | #^/users$#sD                                            |
| Host         | ANY                                                     |
| Host Regex   |                                                         |
| Scheme       | ANY                                                     |
| Method       | ANY                                                     |
| Requirements | NO CUSTOM                                               |
| Class        | Symfony\Component\Routing\Route                         |
| Defaults     | _controller: App\Controller\UserController::index       |
| Options      | compiler_class: Symfony\Component\Routing\RouteCompiler |
+--------------+---------------------------------------------------------+

If you want to find out which URL-path matches to which route you can also use the following command:

php bin/console router:match "/users"

Optionally you can specify additional parameters like the method using CLI-option:

php bin/console router:match "/users" --method="POST" --scheme="https" --host="example.com"