08/06/2018 - SYMFONY
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.
├── 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
$ 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 /
-------------------- -------- -------- ------ ------------------------
APP_ENV=dev
APP_SECRET=0f1e1f38c4818774c1661dc0223e270e
home:
path: /
controller: App\Controller\HomeController::index
controllers:
resource: ../../src/Controller/
type: annotation
prefix: /api/v1
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}'
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)%'
services:
_defaults:
autowire: true
autoconfigure: true
public: false
App\Repository\:
resource: '../../src/Repository'
services:
_defaults:
autowire: true
autoconfigure: true
public: false
App\Service\:
resource: '../../src/Service'
/**
* @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
{
...
}
}
/**
* @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
{
...
}
}