Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony :HOW TO Create Shared -General- (Helper) used in multiple bundles

I had many helper function -Grouped In Classes - for ( formatting Strings And Dates , URL Helpers ) that i want to use and share in several bundles , i need to know best practice about where i can put those helpers functions to be shared between bundles.

What came to my mind is to create a helper bundle and use this bundle in the other bundle that i have in my project or use the vendor helper.

So how i can do this and what is the best practice for Creating Shared Helper to be used in multiple bundles.

Please if there is any references i can look at please share it with me.

Thank you in advance.

like image 869
SMSM Avatar asked Oct 02 '22 08:10

SMSM


2 Answers

Best practice would be to create a PHP library containing those classes. If you really need Symfony integration (eg. DIC configuration), then create bundle that depends on this library.

Every bundle that uses your bundle must list it among it's dependences in composer.json. So it will be installed autocratically every time you install bundle that depends on it.

like image 171
Michał Pipa Avatar answered Oct 13 '22 11:10

Michał Pipa


There are plenty of great examples of libraries out there, that can be imported using composer and used even if they aren't bundles per se, take a look at Doctrine\Common for example.

Regardless, you can also create the bundle as you would any other bundle in Symfony, and structure the code as you see fit. You will notice with many of Doctrine's bundles will make use of the shared library Doctrine\Common.

like image 44
Sean Quinn Avatar answered Oct 13 '22 12:10

Sean Quinn