Symfony 使用 yaml 和 php 路由 [英] Symfony using yaml and php routing

查看:39
本文介绍了Symfony 使用 yaml 和 php 路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Symfony (3.3.8) 应用程序中混合使用 yaml 和 php 路由.我对 yaml 路由非常熟悉,所以我使用 bin/console 原则:generate:crud 命令来查看 PHP 路由是什么样的.它生成了一个看起来像

I would like to use a mix of yaml and php routing in a Symfony (3.3.8) app. I am pretty comfortable with yaml routing, so I used the bin/console doctrine:generate:crud command to see what PHP routing would look like. It generated a routing file that looks like

<?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 文档,您可以在其中使用 Route 构建一个 RouteCollection,然后返回它.

This follows the format suggested on the Symfony docs, where you build up a RouteCollection with Routes and then return it.

当我尝试使用此文件运行我的应用程序时,即使它没有从我的主 routing.yml 文件中引用,我也会收到此错误:

When I try to run my app with this file in place, even if it is not referenced from my main routing.yml file, I get this error:

 [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 文档中建议的格式?或者我是否需要以某种方式告诉自动加载器忽略这个文件,这样它就不会试图找到一个不应该存在的类?

Do I need to redesign this file to act like a class, going against the suggested format in the Symfony docs? Or do I need to somehow tell the autoloader to ignore this file so it doesn't try to find a class where there shouldn't be one?

推荐答案

Symfony 3.3 中引入的重大变化之一是自动连接服务的概念.以我不那么谦虚的观点,很多很多不一致的魔法却收效甚微.

One of the big changes introduced in Symfony 3.3 was the notion of automatically wiring up services. In my not so humble opinion, lots and lots of inconsistent magic for little benefit.

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

As part of the auto wiring process, it was decided to make every class a service by default. Any php file is assumed to have a class in it. Hence the attempt to find a class in Resources/config/routing/restful_resource.php.

为了防止这种行为,您需要明确告诉服务生成器跳过目录.

To prevent this behavior, you need to explicitly tell the service generator to skip directories.

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

引入所有这些新的助手"功能的好处在于,我可以赢得相当多的代表来解释它们.

The good thing about introducing all these new "helper" functions is that I get to earn quite a bit of rep explaining them.

这篇关于Symfony 使用 yaml 和 php 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆