Aşağıdaki örneği kullanarak Symfony 4 uygulamalarında route, servis ve parametre yapılandırma dosyalarını düzenleyebilirsiniz. İsteğinize göre değişiklik yapabilirsiniz.


Yapı


├── 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

Routeler


$ 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 /
-------------------- -------- -------- ------ ------------------------

Dosyalar


.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
{
...
}
}