BentCoder
BentCoder

Reputation: 12740

Avoid dupplicating files and services if there are more than one bundles in the application

I just want to know what is the best practise/way of doing this. I'm going to explain with an example to make it easier to understand.

Note: So far example below work fine if I have only one bundle in my application. Question is at the bottom of the post.

Thanks in advance

SingleBundle/Resources/config/services.yml

services:
    form_errors:
        class:  Hello\SingleBundle\Services\FormErrors

FormErrors.php

namespace Hello\SingleBundle\Services;

use Symfony\Component\Form\FormInterface;

class FormErrors
{
    public function getErrors(FormInterface $form)
    {
        .......
        .......
        return $errors;
    }
}

QUESTION: How do I avoid duplicating these two files if I have more than one bundles in my application? Where do I define service and the service class whcih will be accesible from all the bundles?

Upvotes: 0

Views: 30

Answers (1)

Alex
Alex

Reputation: 1183

You have to define your service in the bundle which implements logic of this service. If you have bundle SingleBundle the best way to call the service is to use special prefix (single_bundle.form_errors in your case). If you have two or more bundles it's not necessary to duplicate service definition and service class declaration because all services defined in the namespace of one bundle (which is properly loaded to the project) are accessible in the namespace of the other bundle (which is properly loaded to the project as well).

So, I think before create a service you just need to think where it should be defined. And take care about service name if you have any doubts about possible duplicates.

Upvotes: 1

Related Questions