是否可以覆盖 Symfony2 中的路由处理程序?
Is it possible to override a route handler in Symfony2?
假设一个模板应用程序是用 Symfony2 创建的。它包含一个产品描述页面,路由 created/handled 如下:
class ProductController extends Controller
{
/**
* @Route("/product/{id}")
*/
public function showDecription($id)
{
// ...
}
}
自定义应用是基于模板应用创建的。也就是说,模板应用提供了'a default value'.
是否可以在不修改模板应用程序代码的情况下覆盖自定义应用程序中的路由处理程序?如果路由需要在模板应用程序中以不同的方式声明以便被覆盖,那没关系。这种情况应该怎么处理?
主路由文件是app/config/routing.yml
。我建议您再创建 2 个文件:app/config/routing_default.yml
和 app/config/routing_override.yml
,然后将 app/config/routing.yml
编辑为
override:
resource: routing_override.yml
default:
resource: routing_default.yml
现在在默认路由中,您可以添加所有默认应用程序路由。并覆盖一个 - 所有其他的。
此外,override 和 default 中的路由名称不应相同。如果你想按名称覆盖路由,你应该更改包含的顺序。
Symfony guide 有以下关于覆盖路由的信息:
Routing
Routing is never automatically imported in Symfony. If you want to
include the routes from any bundle, then they must be manually
imported from somewhere in your application (e.g.
app/config/routing.yml).
The easiest way to "override" a bundle's routing is to never import it
at all. Instead of importing a third-party bundle's routing, simply
copy that routing file into your application, modify it, and import it
instead.
假设一个模板应用程序是用 Symfony2 创建的。它包含一个产品描述页面,路由 created/handled 如下:
class ProductController extends Controller
{
/**
* @Route("/product/{id}")
*/
public function showDecription($id)
{
// ...
}
}
自定义应用是基于模板应用创建的。也就是说,模板应用提供了'a default value'.
是否可以在不修改模板应用程序代码的情况下覆盖自定义应用程序中的路由处理程序?如果路由需要在模板应用程序中以不同的方式声明以便被覆盖,那没关系。这种情况应该怎么处理?
主路由文件是app/config/routing.yml
。我建议您再创建 2 个文件:app/config/routing_default.yml
和 app/config/routing_override.yml
,然后将 app/config/routing.yml
编辑为
override:
resource: routing_override.yml
default:
resource: routing_default.yml
现在在默认路由中,您可以添加所有默认应用程序路由。并覆盖一个 - 所有其他的。
此外,override 和 default 中的路由名称不应相同。如果你想按名称覆盖路由,你应该更改包含的顺序。
Symfony guide 有以下关于覆盖路由的信息:
Routing
Routing is never automatically imported in Symfony. If you want to include the routes from any bundle, then they must be manually imported from somewhere in your application (e.g. app/config/routing.yml).
The easiest way to "override" a bundle's routing is to never import it at all. Instead of importing a third-party bundle's routing, simply copy that routing file into your application, modify it, and import it instead.