Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

You can use example below to organise your route, service and parameter configuration files in Symfony 4 applications. Feel free to change it as you wish.


Structure


├── config
│   ├── routes
│   │   └── annotations.yaml
│   ├── routes.yaml
│   ├── services
│   │   ├── controllers.yaml
│   │   ├── repositories.yaml
│   │   └── services.yaml
│   └── services.yaml
├── src
│   ├── Controller
│   │   ├── CountryController.php
│   │   └── HomeController.php
│   ├── Entity
│   │   └── Country.php
│   ├── Kernel.php
│   ├── Repository
│   │   ├── CountryRepositoryInterface.php
│   │   └── CountryRepository.php
│   └── Service
│   ├── CountryServiceInterface.php
│   └── CountryService.php
└── .env

Routes


$ bin/console debug:router
-------------------- -------- -------- ------ ------------------------
Name Method Scheme Host Path
-------------------- -------- -------- ------ ------------------------
app_country_getall GET ANY ANY /api/v1/countries
app_country_getone GET ANY ANY /api/v1/countries/{id}
home ANY ANY ANY /
-------------------- -------- -------- ------ ------------------------

Files


.env


APP_ENV=dev
APP_SECRET=0f1e1f38c4818774c1661dc0223e270e

config/routes.yaml


home:
path: /
controller: App\Controller\HomeController::index

config/routes/annotations.yaml


controllers:
resource: ../../src/Controller/
type: annotation
prefix: /api/v1

config/services.yaml


imports:
- { resource: services/* }

parameters:
static_param: 'static_param'

services:
_defaults:
autowire: true
autoconfigure: true
public: false

App\:
resource: '../src/*'
exclude: '../src/{Controller,Entity,Repository,Service,Kernel.php}'

config/services/controllers.yaml


services:
_defaults:
autowire: true
autoconfigure: true
public: true

App\Controller\:
resource: '../../src/Controller'
tags: ['controller.service_arguments']

App\Controller\HomeController:
arguments:
$staticParam: '%static_param%'
$envParam: '%env(APP_SECRET)%'

config/services/repositories.yaml


services:
_defaults:
autowire: true
autoconfigure: true
public: false

App\Repository\:
resource: '../../src/Repository'

config/services/services.yaml


services:
_defaults:
autowire: true
autoconfigure: true
public: false

App\Service\:
resource: '../../src/Service'

HomeController


/**
* @Route("/")
*/
class HomeController
{
private $staticParam;
private $envParam;

public function __construct(
string $staticParam,
string $envParam
) {
$this->staticParam = $staticParam;
$this->envParam = $envParam;
}

/**
* @Method({"GET"})
*/
public function index(): Response
{
...
}
}

CountryController


/**
* @Route("/countries")
*/
class CountryController
{
private $countryService;

public function __construct(CountryServiceInterface $countryService)
{
$this->countryService = $countryService;
}

/**
* @Route("")
* @Method({"GET"})
*/
public function getAll(): Response
{
...
}

/**
* @Route("/{id}")
* @Method({"GET"})
*/
public function getOne(int $id): Response
{
...
}
}