Symfony 版本 5.4 和 PHP 版本 7.4。*
在普通的 Symfony 项目中,我可以创建一个带有路由(注释)的控制器,并且它工作得很好。现在我已经创建了一个包,通过 Composer 将其放入测试项目中。在此捆绑包中,我创建了一个带有方法和路由(注释)的控制器。
当我使用 debug:router 时,不会显示捆绑包的路由。我不确定如何配置此捆绑包以便能够在放入它的任何项目上使用控制器路由。
我可以访问 Helper 类,但我不知道如何将控制器与路由一起使用。
捆绑包内的示例
在我的包中,我有一个控制器,它呈现基本的树枝模板:
<?php
namespace MyWork\MyBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class BundlePageController extends AbstractController
{
/**
* @Route("/bundle_index", name="bundle_index")
*
* @return Response
*/
public function index(): Response
{
return $this->render('bundle_page.html.twig');
}
}
当我找到的解决方案说,我必须在路由器配置中绑定它。但它没有奏效。我在我的包中创建了一个routes.yml,想法是从将使用该包的项目的routes.yml中导入这个routes.yml。但我不知道参数/命名,基本上我不知道该放什么。
我也尝试过使用 symfony 文档,但我仍然在挣扎。
我是否必须将此控制器作为服务加载到 service.yml 中?我有点迷失了,感谢任何帮助。
编辑:我在这里找到了一篇文章Symfony 5 Reusable Bundles控制器没有容器集,您是否忘记将其定义为服务订阅者
这里,控制器已添加到服务conf文件和路由conf文件中。我会尝试使用这个。
编辑:现在我添加了以下内容
捆绑服务 xml
<service
id="mywork_mybundle.controller.bundle_page_controller"
class="MyWork\MyBundle\Controller\BundlePageController"
public="true"/>
<service
id="MyWork\MyBundle\Controller\BundlePageController"
alias="mywork_mybundle.controller.bundle_page_controller"/>
捆绑路由 xml
<route
id="mywork_mybundle_bundle_page_controller"
path="/bundle-page"
controller="mywork_mybundle.controller.bundle_page_controller"/>
现在我必须弄清楚如何在项目中实现这一点。
编辑: 路线现在显示在调试:路由器中,但有错误
我已将 paths.xml 更改为 yaml,因为在项目 yaml 文件中导入它时遇到问题。所以现在在捆绑包中我有这个routes.yaml文件:
show_list:
path: /bundle/page/list
controller: MyWork\MyBundle\Controller\BundlePageController::list
但是,当我尝试访问此路线时,出现以下错误 URI“/bundle/page/list”的控制器不可调用:控制器“MyWork\MyBundle\Controller\BundlePageController”无法从容器中获取,因为它是私有的。您是否忘记使用“controller.service_arguments”标记服务?
这是否意味着它无法再读取service.xml,因为我将routes.xml更改为yaml?如之前所述,服务包含 true 的公钥。所以应该是公开的。我不知道是什么导致了这里的问题。
编辑: 现在在捆绑服务 xml 中我创建了这一行:
<service id="Symfony\Bundle\FrameworkBundle\Controller\AbstractController">
<tag name="controller.abstract_controller"/>
</service>
并将其作为参数传递给我的控制器:
<service
id="mywork_mybundle.controller.bundle_page_controller"
class="MyWork\MyBundle\Controller\BundlePageController"
public="true">
<argument type="service" id="controller.abstract_controller"/>
</service>
<service
id="MyWork\MyBundle\Controller\BundlePageController"
alias="mywork_mybundle.controller.bundle_page_controller"/>
我的想法是,也许我必须先加载抽象控制器服务并将其交给我的控制器。但说实话,我不知道...
现在我得到“...mycontroller...”没有容器集,您是否忘记将其定义为服务订阅者?
项目的
config/routes.xml
应 导入 包的路由文件。以下是“如何包含外部路由资源”中给出的示例:
<!-- config/routes.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">
<!-- loads routes from the given routing file stored in some bundle -->
<import resource="@AcmeOtherBundle/Resources/config/routing.yml" />
<!-- loads routes from the PHP annotations of the controllers
found in that directory -->
<import resource="../src/Controller/" type="annotation" />
<!-- loads routes from the YAML or XML files found in that directory -->
<import resource="../legacy/routing/" type="directory" />
<!-- loads routes from the YAML or XML files
found in some bundle directory -->
<import resource="@AppBundle/Resources/config/routing/public/"
type="directory" />
</routes>