没有 app/config/routing.yaml 的 Symfony 路由 [英] Symfony Routes without app/config/routing.yaml

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

问题描述

我读过的 Symfony 2 教程指导用户在

The tutorials I've read for Symfony 2 instructs users to enter their routing information in

app/config/routing.yml

如果用户希望在自己的 bundle 中包含路由信息,他们会被指示添加一个 routing.yml 文件到他们的 Bundle,然后从 app/config 指向他们的文件/routing.yml 类似

If users want to have routing information in their own bundles, they're instructed to add a routing.yml file to their Bundle, and then point to their file from app/config/routing.yml with something like

my_route_stuff:
    resource: "@CustomstuffBundle/resources/config/routing.yml"

有什么办法可以跳过将这个额外的配置添加到 app/config/routing.yml 文件中?我正在寻找将包交给其他人的能力,并且让他们能够将其部署到他们的 Symfony 应用程序中,而无需编辑他们自己的 app/config/routing.yml.

Is there any way to skip the "add this extra configuration to the app/config/routing.yml file? I'm looking for the ability to hand off a bundle to someone else, and have them be able to deploy it into their Symfony application without needing to edit their own app/config/routing.yml.

如果这是不可能的,如果有人能解释为什么(即:背后的一般哲学)路由信息是 AppKernel 而不是单个 Bundles 的一部分,则加分.我仍然有点不清楚 routing.yml 文件和普通 Symfony config.yml 文件之间的区别.

If this isn't possible, bonus point if anyone can explain why (i.e: the general philosophy behind) routing information is part of the AppKernel instead of the individual Bundles. I'm still a little unclear on the differences between routing.yml files and the normal Symfony config.yml files.

推荐答案

有什么办法可以跳过将这个额外的配置添加到 app/config/routing.yml 文件中?

不,这是SonataAdminBundleFOSUserBundle 和其他一些人处理它.

No, this is the way SonataAdminBundle, FOSUserBundle and a bunch of others handle it.

为什么?

路由属于应用程序,而不是每个包.如果每个包都开始包含它们自己的路由文件并且 Symfony2 自动加载它们,那么您很快就会在应用程序中启用或不想启用一堆路由.

Routing belongs to the application, not each and every bundle. If every bundle started including their own routing files and Symfony2 autoloaded them, you would quickly have a mess of routes you may or may not want to enable in your application.

如果 SonataAdminBundle 希望您使用 /admin,但您已经在那里有一个路由并且希望 Sonata 使用 /sonata/admin 来代替怎么办?你需要一个文件来覆盖这些路由,然后你又回到了原点!

What if SonataAdminBundle wanted you to use /admin, but you already had a route there and wanted Sonata to use /sonata/admin instead? You'd need a file to override those routes and then you're back to square one!

此外,虽然缓存减轻了这一部分,但查找文件的成本很高,并且会显着降低开发环境的速度.这就是为什么即使在开发模式下也从缓存中读取翻译文件,并且在添加新的翻译资源时必须清除缓存.请参阅:http://symfony.com/doc/current/book/translation.html#message-catalogues

Additionally, although caching mitigates this part, looking up files is expensive and would significantly slow down the development environment. This is why translation files are read from cache even in dev mode and you must clear the cache when you add a new translation resource. See: http://symfony.com/doc/current/book/translation.html#message-catalogues

最后,将路由排除在 config.yml 之外只是组织问题.路由和配置是两个不同的东西,不属于同一个文件.

Finally, leaving the routing out of config.yml is simply a matter of organization. Routing and configuration are two different things and don't belong in the same file.

总体思路是每个文件都是一个事物,并且应该只做那个事物.

The general idea is that every file is a thing and should only do that thing.

这篇关于没有 app/config/routing.yaml 的 Symfony 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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