来自Symfony框架的新手。我试图通过How to Create a custom Route Loader Cookbook部分,让路由自动加载工作在我的捆绑中,但没有成功。以下都与此主题文件相关:
装载机:
namespace Notimeo\CoreBundle\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 = '@NotimeoCoreBundle/Resources/config/import_routing.yml';
$type = 'yaml';
$importedRoutes = $this->import($resource, $type);
$collection->addCollection($importedRoutes);
return $collection;
}
public function supports($resource, $type = null)
{
return 'advanced_extra' === $type;
}
}
DependencyInjection:
namespace Notimeo\CoreBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
class CoreExtension extends Extension implements PrependExtensionInterface
{
public function load(array $configs, ContainerBuilder $container)
{
}
public function prepend(ContainerBuilder $container)
{
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__.'/../Resources/config')
);
$loader->load('services.yml');
}
}
的src / Notimeo / CoreBundle /资源/配置/ routing.yml中:
notimeo_core_routes:
resource: .
type: advanced_extra
的src / Notimeo / CoreBundle /资源/配置/ import_routing.yml:
# homepage
homepage:
path: /
defaults:
_controller: CoreBundle:Base:index
的src / Notimeo / CoreBundle /资源/配置/ services.yml:
services:
routing.loader.advanced_loader:
class: Notimeo\CoreBundle\Routing\AdvancedLoader
tags:
- { name: routing.loader }
获取No route found for "GET /"
错误...
(使用Symfony 3)
更新:
嗯......如果我要移动 src / Notimeo / CoreBundle / Resources / config / routing.yml 的内容,它的工作正常
到
应用/配置/ routing.yml中
但是......为什么?为什么我无法将所有内容放入我的捆绑包中?
答案 0 :(得分:2)
您可以将所有内容放在src/Notimeo/CoreBundle/Resources/config/routing.yml
中,只需确保将其包含在app/config/routing.yml
只需在app/config/routing.yml
_
my_custom_route:
resource: "@NotimeoCoreBundle/Resources/config/routing.yml"