Enable Custom Features Best Practices in Symfony2

I am new to Symfony2 and I cannot figure out where I should include, including my own array_merge_overwrite project functions (e.g. array_merge_overwrite , array_last , etc.)? I use both types of applications: web (MVC) and console (extends ContainerAwareCommand ).

Or is there another β€œright way” for this?

+6
source share
2 answers

Create a service and include your common functions in it. For example, you can name it ArrayService and register it in the container as array.service . Then you can access this service from the controllers through

 $this->get('array.service'); 

and from teams through

 $this->getContainer()->get('array.service'); 

So your code will look something like this:

 $element = $this->get('array.service')->last($array); // or ->arrayLast($array) 

If you need the same functionality in several projects, create a package with this service and add it to the deps file of each project. It will then be installed when running bin/vendors install script.

+7
source

You can convert your functions to static methods of some class to make them autoload. Or ... well ... Put them where you want, and demand () from where you need them every time.

0
source

Source: https://habr.com/ru/post/901484/


All Articles