如何告诉 symfony 加载我的自定义 routing.yml 配置
How to tell symfony to load my custom routing.yml configuration
我正在努力实现这一目标
http://symfony.com/doc/current/cookbook/routing/custom_route_loader.html#more-advanced-loaders
我需要 bundle 路由在 bundle 注册时自动激活
所以我在路径
中创建了这个文件
src/Gabriel\AdminPanelBundle\Routing\AdvancedLoader.php
含内容
<?php
//namespace Acme\DemoBundle\Routing;
namespace Gabriel\AdminPanelBundle\Routing;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Routing\RouteCollection;
class AdvancedLoader extends Loader
{
public function load($resource, $type = null)
{
$collection = new RouteCollection();
$resource = '@GabrielAdminPanelBundle/Resources/config/import_routing.yml';
$type = 'yaml';
$importedRoutes = $this->import($resource, $type);
$collection->addCollection($importedRoutes);
return $collection;
}
public function supports($resource, $type = null)
{
return $type === 'advanced_extra';
}
}
我复制了这个配置
gabriel_admin_panel:
resource: "@GabrielAdminPanelBundle/Controller/"
type: annotation
prefix: /superuser
来自
/app/config/routing.yml
并将其粘贴到我自己的配置文件中
/src/Gabriel/AdminPanelBundle/Resources/config/import_routing.yml
问题:
Symfony2 completely ignores my AdvancedLoader.php file, I can put any
syntax error in it and the site won't even throw an error, also the
router:debug doesn't show the routes that are defined inside of the
bundle unless I move the configuration back into its original router.yml file.
PS:清除缓存不会改变任何东西
编辑:当我添加服务和资源时,出现这个错误
FileLoaderImportCircularReferenceException: Circular reference
detected in "/app/config/routing_dev.yml"
("/app/config/routing_dev.yml" > "/app/config/routing.yml" > "." >
"@GabrielAdminPanelBundle/Controller/" >
"/app/config/routing_dev.yml").
看起来你可能错过了这个过程中的一些步骤。
第一个:你定义了服务吗?
services:
gabriel.routing_loader:
class: Gabriel\AdminPanelBundle\Routing\AdvancedLoader
tags:
- { name: routing.loader }
记下标签。正如文档所说:
Notice the tag routing.loader. All services with this tag will be
marked as potential route loaders and added as specialized routers to
the DelegatingLoader.
其次但非常重要,因为如文档所述,如果您不添加此行,您的路由加载程序将不会被调用:
# app/config/routing.yml
Gabriel_Extra:
resource: .
type: advanced_extra
这里的重要部分是类型键。在您的情况下,它的值应该是 "advanced_extra" 。这是您的 AdvancedLoader
支持的类型,这将确保调用其 load() 方法。资源键对于AdvancedLoader
来说是无意义的,所以设置为“.”。
我认为它现在会加载。
我正在努力实现这一目标 http://symfony.com/doc/current/cookbook/routing/custom_route_loader.html#more-advanced-loaders
我需要 bundle 路由在 bundle 注册时自动激活
所以我在路径
中创建了这个文件src/Gabriel\AdminPanelBundle\Routing\AdvancedLoader.php
含内容
<?php
//namespace Acme\DemoBundle\Routing;
namespace Gabriel\AdminPanelBundle\Routing;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Routing\RouteCollection;
class AdvancedLoader extends Loader
{
public function load($resource, $type = null)
{
$collection = new RouteCollection();
$resource = '@GabrielAdminPanelBundle/Resources/config/import_routing.yml';
$type = 'yaml';
$importedRoutes = $this->import($resource, $type);
$collection->addCollection($importedRoutes);
return $collection;
}
public function supports($resource, $type = null)
{
return $type === 'advanced_extra';
}
}
我复制了这个配置
gabriel_admin_panel:
resource: "@GabrielAdminPanelBundle/Controller/"
type: annotation
prefix: /superuser
来自
/app/config/routing.yml
并将其粘贴到我自己的配置文件中
/src/Gabriel/AdminPanelBundle/Resources/config/import_routing.yml
问题:
Symfony2 completely ignores my AdvancedLoader.php file, I can put any syntax error in it and the site won't even throw an error, also the router:debug doesn't show the routes that are defined inside of the bundle unless I move the configuration back into its original router.yml file.
PS:清除缓存不会改变任何东西
编辑:当我添加服务和资源时,出现这个错误
FileLoaderImportCircularReferenceException: Circular reference detected in "/app/config/routing_dev.yml" ("/app/config/routing_dev.yml" > "/app/config/routing.yml" > "." > "@GabrielAdminPanelBundle/Controller/" > "/app/config/routing_dev.yml").
看起来你可能错过了这个过程中的一些步骤。
第一个:你定义了服务吗?
services:
gabriel.routing_loader:
class: Gabriel\AdminPanelBundle\Routing\AdvancedLoader
tags:
- { name: routing.loader }
记下标签。正如文档所说:
Notice the tag routing.loader. All services with this tag will be marked as potential route loaders and added as specialized routers to the DelegatingLoader.
其次但非常重要,因为如文档所述,如果您不添加此行,您的路由加载程序将不会被调用:
# app/config/routing.yml
Gabriel_Extra:
resource: .
type: advanced_extra
这里的重要部分是类型键。在您的情况下,它的值应该是 "advanced_extra" 。这是您的 AdvancedLoader
支持的类型,这将确保调用其 load() 方法。资源键对于AdvancedLoader
来说是无意义的,所以设置为“.”。
我认为它现在会加载。