Symfony 4 : How to customize non-standard directories

Mouna Ben Hmida
2 min readFeb 17, 2018

--

Symfony 4 is configured to read orm entities , routing annotations and twig file only from the standard directory .

In this tutorial , I will show you how to set up your project to cope with non-standard directories

Supposing we have a new directory called “new_folder” which contains entities , controllers and twig files .

1️⃣ Controller annotations :

If you want routing be provided by annotations , add the path to your controller to annotation.yaml file

#config/routers/annotation.yaml#..some_alias_name:
resource: 'new_folder/Controller/'
type: annotation

2️⃣ ORM Entity :

Register the new directory of orm entities under doctrine.orm.mappings :

#config/packages/doctrine.yaml doctrine:
orm:
mappings:
#..
new_folder:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/new_folder'
prefix: 'new_folder\Entity'
alias: new_folder

If your folder is a bundle folder , turn “is_bundle” attribute to “true”

3️⃣ templates :

#config/packages/twig.yaml
twig:
paths:
'%kernel.project_dir%/templates':

'%kernel.project_dir%/src/new_folder/templates': some_alias

The alias is optional .

4️⃣ Bundles :

Ok .. It’s not recommended to create bundles in Symfony 4
But if you have to do ..
Create a php file with the name of the bundle

<?php
namespace App\new_folder\someBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class someBundle extends Bundle
{
}

Activate your bundle :

#config/bundles return [
#..
App\new_folder\someBundle::class => ['all' => true],

I’m Mouna , developer from Tunisia
This article is written in collaboration with
Mariem Torjmen
Feel free to put comments , questions or suggestions below ❤

--

--