首页 > 解决方案 > 无法加载资源“”。确保有支持“rest”类型的加载器

问题描述

我在使用 Symfony V4.99 和fosrestbundle.

当我跑步时,php bin/console debug:router我得到了这个:

无法加载资源“App\Controller\ListController”。确保有一个支持“rest”类型的加载器。

这是Routes.yaml的代码:

lists:
    type      : rest
    resource  : App\Controller\ListController
    prefix    : api

这是 fos_rest.yaml 的代码:

fos_rest: 
    format_listener:
        rules:
            - { path: ^/,  fallback_format: json, priorities: [ 'json' ] }

    exception:
        enabled: true

    view:
        view_response_listener:  'force'
        formats:
            json: true 

下面是 ListController.php 的代码:

<?php

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;

class ListController extends AbstractFOSRestController
{
    public function getListsAction()
    {

    }
}

标签: phpapisymfonysymfony4fosrestbundle

解决方案


在 symfony 5.* 与 FOSRestBundle 中,路由存在问题,请尝试:

  1. 安装这个https://github.com/handcraftedinthealps/RestRoutingBundle

    #> composer 需要 handcraftedinthealps/rest-routing-bundle

  2. 然后添加到config/bundles.php这一行:

    HandcraftedInTheAlps\RestRoutingBundle\RestRoutingBundle::class => ['all' => true],


推荐阅读