You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Sulu API Bundle路由配置报错:控制器无法调用问题求助

Sulu框架API Bundle路由配置报错排查

报错信息

The controller for URI "/ovio" is not callable: Controller "ovio.controller" does neither exist as service nor as class.

问题根源分析

1. 控制器类名与服务配置不匹配

服务配置文件中指定的控制器类为DigitalWeb\Bundle\OvioBundle\Controller\OvioController,但实际控制器类名为ProjectController,导致服务无法找到对应类,触发报错。

2. 路由配置重复冲突

同时使用了两种路由配置方式:

  • config/routes_websites.yaml中手动配置路由,指向无效的ovio.controller服务
  • 控制器内使用#[Route]注解定义路由
    两种方式混用导致路由解析逻辑混乱,且yaml路由指向的服务不存在。

3. 服务配置冗余与无效标签

  • 自定义的ovio.controller标签无实际作用
  • 手动注入的fos_rest.view_handler.default服务未在控制器中使用,开启autowire="true"后无需手动注入该参数

修正方案

1. 修正服务配置

修改Resources/config/services.xml中的控制器类名,移除冗余配置:

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="ovio.controller" class="DigitalWeb\Bundle\OvioBundle\Controller\ProjectController" autowire="true" public="true">
            <tag name="controller.service_arguments"/>
        </service>
    </services>
</container>

2. 统一路由配置方式

选择其中一种路由配置方式,避免冲突:

方式一:使用注解路由

删除config/routes_websites.yaml中的ovio_bundle配置段,确保Bundle类中开启注解路由加载:

// 你的Bundle类(如OvioBundle.php)
namespace DigitalWeb\Bundle\OvioBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

class OvioBundle extends Bundle
{
    public function configureRoutes(RoutingConfigurator $routes): void
    {
        $routes->import('../src/Controller/', 'annotation');
    }
}

方式二:使用YAML路由

保留config/routes_websites.yaml,修改为对应控制器方法,同时删除控制器内的#[Route]注解:

ovio_bundle_api:
    path: /ovio/api
    defaults:
        _controller: ovio.controller:index

ovio_bundle_api_license:
    path: /ovio/api/{license}
    defaults:
        _controller: ovio.controller:indexVehicle

3. 优化控制器代码

移除冗余的Request::createFromGlobals(),改用方法注入Request对象:

namespace DigitalWeb\Bundle\OvioBundle\Controller;

use DigitalWeb\Bundle\OvioBundle\Interface\OvioInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;

#[Route('/ovio', name: 'api_')]
class ProjectController extends AbstractController
{
    #[Route('/api', name: 'api')]
    public function index(OvioInterface $ovioInterface): JsonResponse
    {
        $response = $ovioInterface->api_call($this->getParameter('api_key'));
        return JsonResponse::fromJsonString($response);
    }

    #[Route('/api/{license}', name: 'api_license')]
    public function indexVehicle(OvioInterface $ovioInterface, string $license, Request $request): JsonResponse
    {
        $response = $ovioInterface->api_call($this->getParameter('api_key'), $license, $request->query);
        return JsonResponse::fromJsonString($response);
    }
}

内容的提问来源于stack exchange,提问作者Roan Meijer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 00:15:01