php - 如何告诉 symfony 加载我的自定义 routing.yml 配置

标签 php symfony routes symfony-routing

我正在努力实现这一目标 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 来说是无关紧要的,所以它被设置为“.”。

我认为它现在会加载。

关于php - 如何告诉 symfony 加载我的自定义 routing.yml 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28073486/

相关文章:

symfony - 覆盖属性并使其可在 Doctrine Translatable/Symfony2 中进行翻译

javascript - AngularJS 与 RequireJS 路由删除哈希

php - 在 PHP/MySQL 中编写多个查询的最准确方法是什么?为什么?

PHP:为什么 ERROR 常量会产生错误?

php - Ubuntu 允许 www-data 写入新文件

angular - Angular2 中的路由器导航和查询参数 [RC5]

ruby-on-rails - 具有自定义路线的Rails自定义错误页面

php - preg_replace href anchor 与 anchor 文本

php - 如何在 Symfony 中注销被禁止的用户?

php - 在数据库中保存数据时,Doctrine 将 UTF8 编码更改为 html 实体