I am a newbie in Symfony2 and I can't understand where I should make includes with my custom cross-projects functions (e.g. array_merge_overwrite
, array_last
, etc.)? I use both types of apps: web (MVC) and console (extends ContainerAwareCommand
).
Or there is another "right way" for this?
Create a service and put your common functionality in it. For example, you can name it ArrayService
and register it in the container as array.service
. You can then access this service from controllers via
$this->get('array.service');
and from commands via
$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 across several projects, make a bundle with that service and add it to the deps
file of each project. Then it will be installed when you run the bin/vendors install
script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With