如何修复 YAML 文件上的 "Circular reference" 错误?

How to fix "Circular reference" error on YAML file?

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").

我正在努力实现这一目标: http://symfony.com/doc/current/cookbook/routing/custom_route_loader.html#more-advanced-loaders

所以我创建了这个文件

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/routing.yml';
        $type = 'yaml';

        $importedRoutes = $this->import($resource, $type);

        $collection->addCollection($importedRoutes);

        return $collection;
    }

    public function supports($resource, $type = null)
    {
        return $type === 'advanced_extra';
    }
}

/src/Gabriel/AdminPanelBundle/Resources/config/services.yml

services:
    gabriel.routing_loader:
        class: Gabriel\AdminPanelBundle\Routing\AdvancedLoader
        tags:
            - { name: routing.loader }

/app/config/routing.yml

gabriel_messaging:
    resource: "@GabrielMessagingBundle/Controller/"
    type:     annotation
    prefix:   /

fos_js_routing:
    resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml"

# app/config/routing.yml
Gabriel_Extra:
    resource: .
    type: advanced_extra

app/config/routing_dev.yml

_wdt:
    resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
    prefix:   /_wdt

_profiler:
    resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
    prefix:   /_profiler

_configurator:
    resource: "@SensioDistributionBundle/Resources/config/routing/webconfigurator.xml"
    prefix:   /_configurator

_main:
    resource: routing.yml

/src/Gabriel/AdminPanelBundle/Resources/config/routing.yml

gabriel_admin_panel:
    resource: "@GabrielAdminPanelBundle/Controller/"
    type:     advanced_extra
    prefix:   /superuser

仔细看看@GabrielAdminPanelBundle/Resources/config/routing.yml:

gabriel_admin_panel:
    resource: "@GabrielAdminPanelBundle/Controller/"
    type:     advanced_extra
    prefix:   /superuser

type 指定应该使用哪个加载程序,在这种情况下你说 advanced_extra,这是你的加载程序。您的加载程序再次包含此文件,该文件将再次执行加载程序,这将永远持续下去(换句话说:循环引用)。


另请注意,您已经在 app/config/routing.yml 中包含了路线:

gabriel_messaging:
    resource: "@GabrielMessagingBundle/Controller/"
    type:     annotation
    prefix:   /

这一次,您使用了正确的类型:annotation。您应该删除此条目并编辑 @GabrielAdminPanelBundle/Resources/config/routing.yml 文件以使用正确的类型。