繁体   English   中英

Symfony使用yaml和php路由

[英]Symfony using yaml and php routing

我想在Symfony(3.3.8)应用程序中混合使用yaml和php路由。 我对yaml路由非常满意,因此我使用bin/console doctrine:generate:crud命令来查看PHP路由的外观。 它生成了一个路由文件,看起来像

<?php

use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;

$collection = new RouteCollection();

$collection->add('user_index', new Route(
    '/',
    array('_controller' => 'AppBundle:User:index'),
    array(),
    array(),
    '',
    array(),
    array('GET')
));

// other CRUD routes...

return $collection;

这遵循Symfony docs上建议格式 ,您可以在RouteCollection使用Route创建一个RouteCollection ,然后将其返回。

当我尝试使用该文件运行我的应用程序时,即使未从我的主routing.yml文件中引用该文件,也出现此错误:

 [Symfony\Component\Config\Exception\FileLoaderLoadException]
  The autoloader expected class "AppBundle\Resources\config\routing\restful_resource" to be defined i
  n file "/home/username/sites/appname/vendor/composer/../../src/AppBundle/Resources/config/routing/restfu
  l_resource.php". The file was found but the class was not in it, the class name or namespace probab
  ly has a typo in /home/username/sites/appname/app/config/services.yml (which is being imported from "/ho
  me/username/sites/appname/app/config/config.yml").



  [RuntimeException]
  The autoloader expected class "AppBundle\Resources\config\routing\restful_resource" to be defined i
  n file "/home/username/sites/appname/vendor/composer/../../src/AppBundle/Resources/config/routing/restfu
  l_resource.php". The file was found but the class was not in it, the class name or namespace probab
  ly has a typo.

我是否需要重新设计此文件以使其像类一样,与Symfony文档中建议的格式背道而驰? 还是我需要以某种方式告诉自动加载器忽略此文件,以便它不会尝试在不应存在的类中查找该类?

Symfony 3.3中引入的一项重大更改是自动连接服务的概念。 在我不那么卑鄙的观点中,很多不一致的魔术毫无用处。

作为自动接线过程的一部分,默认情况下决定使每个类成为一项服务。 假定任何php文件中都有一个类。 因此,尝试在Resources / config / routing / restful_resource.php中找到一个类。

为防止此行为,您需要明确告知服务生成器跳过目录。

// app/config/services.yml
AppBundle\:
    resource: '../../src/AppBundle/*'
    exclude: '../../src/AppBundle/{Entity,Repository,Tests,Resources}'

引入所有这些新的“帮助器”功能的好处是,我获得了很多解释它们的代表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM