gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 23:30:37 24 4
gpt4 key购买 nike

我正在努力实现这一目标 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com