How Symfony 2 extends third-party solutions
淡淡烟草味
淡淡烟草味 2017-05-16 16:45:49
0
1
287

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?

淡淡烟草味
淡淡烟草味

reply all (1)
黄舟

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.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!