我正在嘗試在我的 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”沒有設定容器,是不是忘記定義為服務訂閱者了?
我嘗試通過洗掉 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
我不明白為什么會這樣?
Any help or guidance would be greatly appreciated. Let me know should I need to include additional info.
uj5u.com熱心網友回復:
我通過執行以下幾個步驟設法讓我的控制器作業:
- 從 services.yaml 中洗掉 EntryController 的服務定義
- 擴展抽象控制器
類 EntryController 擴展了 AbstractController
啟用自動裝配并在我的 services.yaml config/services.yaml 中公開控制器
App\Controller: 資源: '../src/Controller' public: true autowire: true 標簽: ['controller.service_arguments']
uj5u.com熱心網友回復:
你的類必須從“use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;”擴展“AbstractController”
您可以查看檔案:
https://symfony.com/doc/current/controller.html#the-base-controller-class-services
(通常,默認配置足以解決您的問題。您不需要觸摸它。)
順便說一句,如果您更改了配置,請不要忘記清除快取,以確保安全。
告訴我它是否有效!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/379369.html
標籤:php symfony symfony5 sylius
上一篇:控制器不存在
