简体   繁体   English

是否可以在Symfony2中覆盖路由处理程序?

[英]Is it possible to override a route handler in Symfony2?

Say a template application is created with Symfony2. 假设使用Symfony2创建了模板应用程序。 It contains a product description page and the route is created/handled as following: 它包含一个产品描述页面,并且按照以下方式创建/处理路线:

class ProductController extends Controller
{
    /**
     * @Route("/product/{id}")
     */
    public function showDecription($id)
    {
        // ...
    }
}

Custom applications are created based on the template application. 基于模板应用程序创建自定义应用程序。 In other words, the template application provides 'a default value'. 换句话说,模板应用程序提供“默认值”。

Is it possible to override the route handler in a custom application without modifying the code of the template application? 是否可以在不修改模板应用程序代码的情况下覆盖自定义应用程序中的路由处理程序? If the route needs to be declared in a different way in the template application in order to be overridable, that's ok. 如果需要在模板应用程序中以其他方式声明路由以使其可覆盖,则可以。 How should one proceed in this case? 在这种情况下应该如何进行?

The main routing file is app/config/routing.yml . 主路由文件是app/config/routing.yml I suggest you to create 2 more files: app/config/routing_default.yml and app/config/routing_override.yml and then edit your app/config/routing.yml as 我建议您再创建2个文件: app/config/routing_default.ymlapp/config/routing_override.yml ,然后将app/config/routing.yml编辑为

override:
  resource: routing_override.yml
default:
  resource: routing_default.yml

Now in routing default you can add all default applications routes. 现在,在默认路由中,您可以添加所有默认应用程序路由。 And in override one - all additional ones. 并覆盖一个-所有其他。

Also route names in override and default should not be the same. 此外,覆盖和默认中的路由名称不应相同。 If you want to override route by name you should change the order of includes. 如果要按名称覆盖路由,则应更改包含的顺序。

Symfony guide has the following information on overriding a route: Symfony指南提供了有关覆盖路由的以下信息:

Routing 路由

Routing is never automatically imported in Symfony. 路由永远不会在Symfony中自动导入。 If you want to include the routes from any bundle, then they must be manually imported from somewhere in your application (eg app/config/routing.yml). 如果要包括来自任何捆绑包的路由,则必须从应用程序中的某个位置手动将其导入(例如,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. 无需导入第三方分发包的路由,只需将该路由文件复制到您的应用程序中,对其进行修改,然后将其导入。

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

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