控制器未正确注册为服务?
Controller not being registered as a service correctly?
我正在尝试在我的 Sylius/Symfony 5 设置中创建一个带有管理路由的基本 EntryController 控制器。
我的 src/Controller/EntryController.php 看起来如下:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class EntryController extends AbstractController
{
/**
* @param Request $request
* @return Response
*/
public function indexAction(Request $request): Response
{
dd('THIS CONTROLLER IS WORKING!');
}
}
我的控制器 src/Resources/config/routing/admin/order_form.yml 的路由如下所示:
sylius_complete_order_form:
path: /order/form
methods: [GET]
controller: App\Controller\EntryController::index
并且我的控制器在 config/services.yaml:
中被定义为一个服务
# Controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
App\Controller\:
resource: '../src/Controller'
public: true
autowire: true
tags: ['controller.service_arguments']
目前,当我尝试导航到上面定义的 sylius_complete_order_form 路线的路径时,出现以下错误:
"App\Controller\EntryController" has no container set, did you forget
to define it as a service subscriber?
我尝试通过删除 var/cache
手动清除缓存
文件夹。
我试过了运行宁php bin/console cache:clear
当我运行php bin/console debug:container EntryController
输出如下:
Service ID App\Controller\EntryController
Class App\Controller\EntryController
Tags controller.service_arguments
Calls setContainer
Public yes
Synthetic no
Lazy no
Shared yes
Abstract no
Autowired yes
Autoconfigured no
我不明白为什么会这样?
如有任何帮助或指导,我们将不胜感激。让我知道我是否需要包含其他信息。
您的 class 必须从“使用 Symfony\Bundle\FrameworkBundle\Controller\AbstractController”扩展“AbstractController”;“
您可以查看文档:
https://symfony.com/doc/current/controller.html#the-base-controller-class-services
(通常情况下,默认配置就可以解决你的问题,你应该不需要去碰它。)
顺便说一下,如果你更改了conf,不要忘记清除缓存,以防万一。
告诉我它是否有效!
我已经通过几个步骤让我的控制器工作了:
- 从 services.yaml
中删除 EntryController 的服务定义
- 扩展抽象控制器
class EntryController 扩展了 AbstractController
启用自动装配并使控制器 public 在我的 services.yaml 中
config/services.yaml
App\Controller:
资源:'../src/Controller'
public:真
自动装配:真
标签: ['controller.service_arguments']
我正在尝试在我的 Sylius/Symfony 5 设置中创建一个带有管理路由的基本 EntryController 控制器。
我的 src/Controller/EntryController.php 看起来如下:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class EntryController extends AbstractController
{
/**
* @param Request $request
* @return Response
*/
public function indexAction(Request $request): Response
{
dd('THIS CONTROLLER IS WORKING!');
}
}
我的控制器 src/Resources/config/routing/admin/order_form.yml 的路由如下所示:
sylius_complete_order_form:
path: /order/form
methods: [GET]
controller: App\Controller\EntryController::index
并且我的控制器在 config/services.yaml:
中被定义为一个服务# Controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
App\Controller\:
resource: '../src/Controller'
public: true
autowire: true
tags: ['controller.service_arguments']
目前,当我尝试导航到上面定义的 sylius_complete_order_form 路线的路径时,出现以下错误:
"App\Controller\EntryController" has no container set, did you forget to define it as a service subscriber?
我尝试通过删除 var/cache
手动清除缓存 文件夹。我试过了运行宁
php bin/console cache:clear
当我运行
php bin/console debug:container EntryController
输出如下:Service ID App\Controller\EntryController Class App\Controller\EntryController Tags controller.service_arguments Calls setContainer Public yes Synthetic no Lazy no Shared yes Abstract no Autowired yes Autoconfigured no
我不明白为什么会这样?
如有任何帮助或指导,我们将不胜感激。让我知道我是否需要包含其他信息。
您的 class 必须从“使用 Symfony\Bundle\FrameworkBundle\Controller\AbstractController”扩展“AbstractController”;“
您可以查看文档:
https://symfony.com/doc/current/controller.html#the-base-controller-class-services
(通常情况下,默认配置就可以解决你的问题,你应该不需要去碰它。)
顺便说一下,如果你更改了conf,不要忘记清除缓存,以防万一。
告诉我它是否有效!
我已经通过几个步骤让我的控制器工作了:
- 从 services.yaml 中删除 EntryController 的服务定义
- 扩展抽象控制器
class EntryController 扩展了 AbstractController
启用自动装配并使控制器 public 在我的 services.yaml 中 config/services.yaml
App\Controller: 资源:'../src/Controller' public:真 自动装配:真 标签: ['controller.service_arguments']