概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:43:14 | 219 次阅读 | 评论: 0 | 来源: 网络整理

When working with third-party bundles, you’ll probably come across a situation where you want to override a file in that third-party bundle with a file in one of your own bundles. Symfony gives you a very convenient way to override things like controllers, templates, and other files in a bundle’s Resources/ directory.

For example, suppose that you’re installing the FOSUserBundle, but you want to override its base layout.html.twig template, as well as one of its controllers. Suppose also that you have your own AcmeUserBundle where you want the overridden files to live. Start by registering the FOSUserBundle as the “parent” of your bundle:

// src/Acme/UserBundle/AcmeUserBundle.php
namespace AcmeUserBundle;

use SymfonyComponentHttpKernelBundleBundle;

class AcmeUserBundle extends Bundle
{
    public function getParent()
    {
        return 'FOSUserBundle';
    }
}

By making this simple change, you can now override several parts of the FOSUserBundle simply by creating a file with the same name.

注解

Despite the method name, there is no parent/child relationship between the bundles, it is just a way to extend and override an existing bundle.

Overriding Controllers

Suppose you want to add some functionality to the registerAction of a RegistrationController that lives inside FOSUserBundle. To do so, just create your own RegistrationController.php file, override the bundle’s original method, and change its functionality:

// src/Acme/UserBundle/Controller/RegistrationController.php
namespace AcmeUserBundleController;

use FOSUserBundleControllerRegistrationController as BaseController;

class RegistrationController extends BaseController
{
    public function registerAction()
    {
        $response = parent::registerAction();

        // ... do custom stuff
        return $response;
    }
}

小技巧

Depending on how severely you need to change the behavior, you might call parent::registerAction() or completely replace its logic with your own.

注解

Overriding controllers in this way only works if the bundle refers to the controller using the standard FOSUserBundle:Registration:register syntax in routes and templates. This is the best practice.

Overriding Resources: Templates, Routing, etc

Most resources can also be overridden, simply by creating a file in the same location as your parent bundle.

For example, it’s very common to need to override the FOSUserBundle’s layout.html.twig template so that it uses your application’s base layout. Since the file lives at Resources/views/layout.html.twig in the FOSUserBundle, you can create your own file in the same location of AcmeUserBundle. Symfony will ignore the file that lives inside the FOSUserBundle entirely, and use your file instead.

The same goes for routing files and some other resources.

注解

The overriding of resources only works when you refer to resources with the @FOSUserBundle/Resources/config/routing/security.xml method. If you refer to resources without using the @BundleName shortcut, they can’t be overridden in this way.

警告

Translation and validation files do not work in the same way as described above. Read “Translations” if you want to learn how to override translations and see “Validation Metadata” for tricks to override the validation.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务