php URI的控制器不可调用,控制器既不作为服务也不作为类存在

lyfkaqu1  于 2024-01-05  发布在  PHP
关注(0)|答案(2)|浏览(208)

当我试图调用一个控制器时,标题中出现了错误。
下面是我的配置:

config/services.yaml

  1. parameters:
  2. locale: 'en'
  3. localhost_base_url: '%env(string:localhost_base_url)%'
  4. services:
  5. # default configuration for services in *this* file
  6. _defaults:
  7. autowire: true # Automatically injects dependencies in your services.
  8. autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
  9. public: false
  10. # makes classes in src/MlngAppBundle/ available to be used as services
  11. # this creates a service per class whose id is the fully-qualified class name
  12. MlngApp\MlngAppBundle\:
  13. resource: '../src/MlngApp/MlngAppBundle/*'
  14. exclude:
  15. - '../src/MlngApp/MlngAppBundle/MlngAppBundle.php'
  16. - '../src/MlngApp/MlngAppBundle/{Common,Event,Location,People,User}/Domain/{Entity,Dto,Exception,Invariant}/*'
  17. - '../src/MlngApp/MlngAppBundle/{Common,Event,Location,People,User}/Tests/*'
  18. - '../src/MlngApp/MlngAppBundle/{Common,Event,Location,People,User}/Infrastructure/CommandLine/*'

字符串

config/packages/config.yaml

  1. imports:
  2. - { resource: domains/* }
  3. - { resource: "@MlngAppBundle/Common/Infrastructure/Resources/config/services.xml" }
  4. - { resource: "@MlngAppBundle/Location/Infrastructure/Resources/config/services.xml" }


和“Common”services.xml文件:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <container xmlns="http://symfony.com/schema/dic/services"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
  5. <services>
  6. <!-- repository classes -->
  7. <prototype
  8. namespace="MlngApp\MlngAppBundle\"
  9. resource="../../../../../../../src/MlngApp/MlngAppBundle/{Common,Event,Location,People,User}/Infrastructure/Repository/"
  10. autoconfigure="true"
  11. autowire="true"
  12. public="true"
  13. >
  14. <tag name="doctrine.repository_service"/>
  15. </prototype>
  16. <!-- repository interface classes -->
  17. <prototype
  18. namespace="MlngApp\MlngAppBundle\"
  19. resource="../../../../../../../src/MlngApp/MlngAppBundle/{Common,Event,Location,People,User}/Domain/Repository/"
  20. autoconfigure="true"
  21. autowire="true"
  22. public="true"
  23. >
  24. <tag name="doctrine.repository_service"/>
  25. </prototype>
  26. <!-- controllers -->
  27. <prototype
  28. namespace="MlngApp\MlngAppBundle\"
  29. resource="../../../../../../../src/MlngApp/MlngAppBundle/{Common,Event,Location,People,User}/Api/Controller"
  30. >
  31. <tag name="controller.service_arguments" />
  32. <call method="setContainer">
  33. <argument type="service" id="service_container"/>
  34. </call>
  35. </prototype>
  36. </services>
  37. </container>


关于路线,我有:

config/routes.yaml

  1. location:
  2. resource: "@MlngAppBundle/Location/Api/Resources/config/routing.xml"
  3. prefix: /api
  4. type: xml


@MlngAppBundle/Location/API/Resources/config/routing.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <routes xmlns="http://symfony.com/schema/routing"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"
  5. >
  6. <import resource="routing/location.xml" prefix="/location"/>
  7. <route id="testingstuff" path="/donothing" methods="GET">
  8. <default key="_controller">MlngApp/MlngAppBundle/Location/Api/Controller/AnotherController::anotherEndpoint</default>
  9. </route>
  10. </routes>


以及控制器:

  1. <?php
  2. namespace MlngApp\MlngAppBundle\Location\Api\Controller;
  3. use FOS\RestBundle\Controller\AbstractFOSRestController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. class AnotherController extends AbstractFOSRestController
  6. {
  7. public function __construct()
  8. {
  9. }
  10. public function anotherEndpoint(Request $request) {
  11. echo "do nothing";
  12. }
  13. }


当我转到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
我不知道去哪里找线索。
有什么建议吗?

qni6mghb

qni6mghb1#

谢谢各位。
@msg,你说的对,我用错了反斜线,有点惭愧,这是新手的错误,惭愧。
@MlngAppBundle/Location/Api/Resources/config/routing.xml中,它应该是

  1. <default key="_controller">MlngApp\MlngAppBundleLocation\Api\Controller\AnotherController::anotherEndpoint</default>

字符串
正在工作中。
顺便说一下,我正在尝试将此配置作为一种使用DDD的方式,并使用https://www.fabian-keller.de/blog/domain-driven-design-with-symfony-a-folder-structure/中提出的体系结构设计
再次感谢@Cerad和@msg的宝贵时间和建议。

mwecs4sa

mwecs4sa2#

对我来说,我已经在实体中添加了我的控制器的“use 'Name_Space'”,它工作了!!

相关问题