How extract the domain of your application out of the src directory of Symfony2

A good practice for when you are developing a project is, keep the logical of the business (domain) out of the rest of the application, keeping clean of references to anywhere services or frameworks for reuse your domain on other applications without edit nothing.

But, in case of use Symfony2 framework, it was configure by default for your code are on a bundle in src directory. If you use always Symfony framework, this is not a problem for you but, if you uses other php framworks, probably it use an other structure of directories and you can not use your domain without edit it.

For configure Symfony 2 to use your classes out of the src directory follow the next steps:

Create a directory in to the root project (usually I called Domain but you can put the name that your like) and add all your classes of your domain in it. You don’t forget edit the namespaces of this classes, and all the references of it in the applications that use this classes.

For example, we have this project with the next structure:

View Code SHELL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
app /
bin /
Domain/ 
├── Core 
│   └── Product.php 
├── Exceptions 
│   └── ProductException.php 
├── Factories 
│   └── ProductFactory.php 
└── Tests 
      ├── Core 
      │   └── ProductItemTest.php 
      └── Factories 
        └── ProductFactoryTest.php 
src/ 
└── ProjectTest
    └── TestBundle /
vendor/ 
web/

It is a summary but, it is an example for best understand. On it, you can see that the domain is in of the root project but out of the src directory.

Then, so that Symfony can find the classes of your domain, we need to register on the loader.

We have two ways to do it, depending if the classes of your domain use the standard PSR4 or PSR0.

If it use the standard PSR4, edit the file /app/autoload.php adding the next line before the return.

1
$loader->addPsr4('name_of_the_domain_used_in_the_namespaces\\', 'absolute_route_to_the_classes_of_the_domain');

In the example case:

1
$loader->addPsr4('Domain\\', __DIR__.'/../Domain');

If your classes use the standard PSR0, edit the file /app/autoload.php adding the next line before the return.

1
$loader->add('name_of_the_domain_used_in_the_namespaces\\', 'absolute_route_to_the_classes_of_the_domain');

Then, edit the file composer.json and add to the key autoload the name of the domain and the relative route from the root of your project.

If your domain use the standard PSR4

1
2
3
4
    "autoload": {
        "psr-0": { "": "src/", "SymfonyStandard": "app/" },
        "psr-4": { "name_of_the_domain_used_in_the_namespaces\\": "relative_route_to_the_classes_of_the_domain/"}
    },

In the example case:

1
2
3
4
    "autoload": {
        "psr-0": { "": "src/", "SymfonyStandard": "app/" },
        "psr-4": { "Domain\\": "Domain/"}
    },

But if your domain use the standard PSR0

1
2
3
    "autoload": {
        "psr-0": { "": "src/", "SymfonyStandard": "app/", "name_of_the_domain_used_in_the_namespaces\\": "relative_route_to_the_classes_of_the_domain/"}
    },

So then, execute composer update to update your project. When finish, your project of Symfony 2 will know your domain and you can use your classes on your application.

This configuration was tested on the Symfony 2.6 version. I hope that helps.

Leave a Reply

Your email address will not be published. Required fields are marked *