【发布时间】:2014-08-03 15:13:50
【问题描述】:
我在从捆绑包中自动加载路由时遇到问题。我在文件夹中有 routing.yml 文件:Bundle\UsersBundle\Resources\config。我正在加载他们在 Symfony 食谱中所说的路线(http://symfony.com/doc/current/cookbook/routing/custom_route_loader.html)。这一切都适用于一个捆绑包,但是当我将另一个捆绑包添加到 AppKernel 时,它不会加载新的路由。看起来每个包都需要在 main routing.yml 中单独输入(这正是我不想拥有的)。每个 RoutesLoader 服务都设置了正确的标签,并且容器正在正确加载此服务。
routing.yml:(此配置有效,但它需要来自另一个捆绑包的 RouteLoader 必须返回另一种类型 - “bundle_routes_2” - 当它返回“bundle_routes”时,不会加载路由)
bundles_routes:
resource: .
type: bundle_routes
bundles_routes_2:
resource: .
type: bundle_routes_2
更具体地说——例如我有 3 个捆绑包——UsersBundle、PermissionsBundle 和 GroupsBundle。每个包在其资源文件夹(bundle_folder/Resources/config/routing.yml)中都有路由。每个包都有 RoutesLoader 类,看起来像这样:
<?php
namespace Acme\UsersBundle\Routing;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Routing\RouteCollection;
class UsersRoutesLoader extends Loader
{
public function load($resource, $type = null)
{
$collection = new RouteCollection();
$resource = "@AcmeUsersBundle/Resources/config/routing.yml";
$type = "yaml";
$importedRoutes = $this->import($resource, $type);
$collection->addCollection($importedRoutes);
return $collection;
}
public function supports($resource, $type = null)
{
return $type === "bundle_routes";
}
}
主路由文件看起来像这样:
acme_bundles_routes:
resource: .
type: bundle_routes
但它仅适用于一个捆绑包。我必须将 routing.yml 更改为:
acme_bundles_routes:
resource: .
type: bundle_routes
acme_bundles_routes_2:
resource: .
type: bundle_routes_2
acme_bundles_routes_3:
resource: .
type: bundle_routes_3
每个 RoutesLoader 都必须返回对应的类型。
【问题讨论】:
标签: php symfony routing bundle autoload