当我试图调用一个控制器时,标题中出现了错误。
下面是我的配置:
config/services.yaml
parameters:
locale: 'en'
localhost_base_url: '%env(string:localhost_base_url)%'
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false
# makes classes in src/MlngAppBundle/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
MlngApp\MlngAppBundle\:
resource: '../src/MlngApp/MlngAppBundle/*'
exclude:
- '../src/MlngApp/MlngAppBundle/MlngAppBundle.php'
- '../src/MlngApp/MlngAppBundle/{Common,Event,Location,People,User}/Domain/{Entity,Dto,Exception,Invariant}/*'
- '../src/MlngApp/MlngAppBundle/{Common,Event,Location,People,User}/Tests/*'
- '../src/MlngApp/MlngAppBundle/{Common,Event,Location,People,User}/Infrastructure/CommandLine/*'
字符串
config/packages/config.yaml
imports:
- { resource: domains/* }
- { resource: "@MlngAppBundle/Common/Infrastructure/Resources/config/services.xml" }
- { resource: "@MlngAppBundle/Location/Infrastructure/Resources/config/services.xml" }
型
和“Common”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>
<!-- repository classes -->
<prototype
namespace="MlngApp\MlngAppBundle\"
resource="../../../../../../../src/MlngApp/MlngAppBundle/{Common,Event,Location,People,User}/Infrastructure/Repository/"
autoconfigure="true"
autowire="true"
public="true"
>
<tag name="doctrine.repository_service"/>
</prototype>
<!-- repository interface classes -->
<prototype
namespace="MlngApp\MlngAppBundle\"
resource="../../../../../../../src/MlngApp/MlngAppBundle/{Common,Event,Location,People,User}/Domain/Repository/"
autoconfigure="true"
autowire="true"
public="true"
>
<tag name="doctrine.repository_service"/>
</prototype>
<!-- controllers -->
<prototype
namespace="MlngApp\MlngAppBundle\"
resource="../../../../../../../src/MlngApp/MlngAppBundle/{Common,Event,Location,People,User}/Api/Controller"
>
<tag name="controller.service_arguments" />
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
</prototype>
</services>
</container>
型
关于路线,我有:
config/routes.yaml
location:
resource: "@MlngAppBundle/Location/Api/Resources/config/routing.xml"
prefix: /api
type: xml
型
和
@MlngAppBundle/Location/API/Resources/config/routing.xml
<?xml version="1.0" encoding="UTF-8"?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"
>
<import resource="routing/location.xml" prefix="/location"/>
<route id="testingstuff" path="/donothing" methods="GET">
<default key="_controller">MlngApp/MlngAppBundle/Location/Api/Controller/AnotherController::anotherEndpoint</default>
</route>
</routes>
型
以及控制器:
<?php
namespace MlngApp\MlngAppBundle\Location\Api\Controller;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use Symfony\Component\HttpFoundation\Request;
class AnotherController extends AbstractFOSRestController
{
public function __construct()
{
}
public function anotherEndpoint(Request $request) {
echo "do nothing";
}
}
型
当我转到http://127.0.0.1:8000/api/donothing时,
URI“/API/donothing”的控制器不可调用:控制器“MlngApp/MlngAppBundle/Location/Api/Controller/AnotherController”既不作为服务也不作为类存在。
我理解这意味着AnotherController类是未定义的,但它在services.yaml和services.xml中定义了
使用Symfony 5.4
我不知道去哪里找线索。
有什么建议吗?
2条答案
按热度按时间qni6mghb1#
谢谢各位。
@msg,你说的对,我用错了反斜线,有点惭愧,这是新手的错误,惭愧。
在
@MlngAppBundle/Location/Api/Resources/config/routing.xml
中,它应该是字符串
正在工作中。
顺便说一下,我正在尝试将此配置作为一种使用DDD的方式,并使用https://www.fabian-keller.de/blog/domain-driven-design-with-symfony-a-folder-structure/中提出的体系结构设计
再次感谢@Cerad和@msg的宝贵时间和建议。
mwecs4sa2#
对我来说,我已经在实体中添加了我的控制器的“use 'Name_Space'”,它工作了!!