如何在Symfony 4中找到我的GET /用户路由

pea*_*ove 0 php routing symfony

我收到一条错误消息,没有找到"GET/users"的路由:

级别:错误,频道:请求,消息:未捕获的PHP异常Symfony\Component\HttpKernel\Exception\NotFoundHttpException:"在/ Users/work/project/vendor/symfony/http-kernel /"找不到"GET/users"的路由EventListener/RouterListener.php第139行

但实际上我为"用户"设置了一条路线,所以我不知道如何解决这个问题:

<?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());
    }
  }

}
Run Code Online (Sandbox Code Playgroud)

dbr*_*ann 5

类顶部的注释仅定义前缀,而不是实际路径.当你在类中有多个方法时,为什么会变得清晰.路由器如何识别要呼叫哪一个?

换句话说,您必须将注释移动到方法或将另外一个添加到操作:

<?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');
    }
}
Run Code Online (Sandbox Code Playgroud)

这会将路径定义从动作追加到类中的路径定义,就像人们对前缀路径所期望的那样.

如果您想知道Symfony识别哪些路由,您可以在控制台中使用debug命令:

php bin/console debug:router
Run Code Online (Sandbox Code Playgroud)

或者针对特定路线:

php bin/console debug:router users
Run Code Online (Sandbox Code Playgroud)

这是我在演示应用程序中获得的输出:

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 |
+--------------+---------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

如果要查找哪个URL路径与哪个路由匹配,还可以使用以下命令:

php bin/console router:match "/users"
Run Code Online (Sandbox Code Playgroud)

您可以选择使用CLI-option指定其他参数,例如方法:

php bin/console router:match "/users" --method="POST" --scheme="https" --host="example.com"
Run Code Online (Sandbox Code Playgroud)