首页 > 解决方案 > 如何在 Symfony 4 中找到通往 GET/users 的路线

问题描述

我收到一条错误消息,找不到“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());
    }
  }

}

标签: phpsymfonyrouting

解决方案


类顶部的注释只定义了一个前缀,而不是实际的路由。当您在类中有多个方法时,原因就很清楚了。路由器如何识别呼叫哪一个?

换句话说,您要么必须将注释移动到方法中,要么将这样的另一个注释添加到操作中:

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

这会将路由定义从操作附加到类上的定义,就像人们期望的前缀路由一样。

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

php bin/console debug:router

或针对特定路线:

php bin/console debug:router users

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

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 |
+--------------+---------------------------------------------------------+

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

php bin/console router:match "/users"

或者,您可以指定其他参数,例如使用 CLI 选项的方法:

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

推荐阅读