search

Home  >  Q&A  >  body text

How Symfony 2 extends third-party solutions

How to extend the third party in the directory structure of symfony2?
Do you write it yourself in vendor or is there a prescribed directory format?
Symfony1 provides a helper mode. Does symfony2 also have the same mechanism?

淡淡烟草味淡淡烟草味2753 days ago341

reply all(1)I'll reply

  • 黄舟

    黄舟2017-05-16 16:47:49

    I pasted some code directly from the document:

    To extend a certain bundle, you only need to declare which bundle is being extended through the getParent() method in your own bundle. The following is using FOSUserBundle as the extension object.

    // src/Acme/UserBundle/AcmeUserBundle.php
    namespace Acme\UserBundle;
    
    use Symfony\Component\HttpKernel\Bundle\Bundle;
    
    class AcmeUserBundle extends Bundle
    {
        public function getParent()
        {
            return 'FOSUserBundle';
        }
    }

    Extending a Controller is easier, just inherit the target Controller directly:

    // src/Acme/UserBundle/Controller/RegistrationController.php
    namespace Acme\UserBundle\Controller;
    
    use FOS\UserBundle\Controller\RegistrationController as BaseController;
    
    class RegistrationController extends BaseController
    {
        public function registerAction()
        {
            $response = parent::registerAction();
    
            // ... do custom stuff
            return $response;
        }
    }

    As for other things such as templates, routes, etc., whichever one you want to reuse or rewrite, just create it in your own bundle with the same file path.

    Documentation: http://symfony.com/doc/current/cookbo...

    UPDATE1:

    If you just want to add third-party code, according to the specification, it should be placed in vendors. If your code complies with PSR-0, then automatic loading can be achieved; if not, you have to add it in app/autoload.php Riga the corresponding include.

    As for whether the expanded code can be used globally, it depends on the call, not the definition. For the convenience of maintenance, you can abstract the interface yourself or define the service in sf2.

    You can refer to the sf2 bundle documentation and Composer.

    reply
    0
  • Cancelreply