services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
bind:
$images_directory: '%app.images_directory%'
$lang: '%blabalabla%' # Господа, обратите внимание!
...
public function add(Request $request, EntityManagerInterface $manager, $lang)
...
class Locale
{
/**
* @var string
*/
private $value;
public function __construct(string $value)
{
$this->value = $value;
}
public function __toString()
{
return $this->value;
}
}
class LocaleFactory
{
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var string
*/
private $default;
public function __construct(RequestStack $requestStack, string $default)
{
$this->requestStack = $requestStack;
$this->default = $default;
}
public function create(): Locale
{
if ($request = $this->requestStack->getMasterRequest()) {
return new Locale($request->get('locale', $this->default));
}
return new Locale($this->default);
}
}
services:
App\Locale\Locale:
factory: ['@App\Locale\LocaleFactory', 'create']
public function add(Request $request, EntityManagerInterface $manager, Locale $lang)
parameters:
lang: ru
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
bind:
$default_lang: '%lang%'
services:
app.service.lang:
class: App\Service\Lang
arguments: ['@request_stack', '@?']
<?php
namespace App\Service;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Class Lang
* @package App\Service
*/
class Lang
{
/** @var string */
private $lang;
/**
* Lang constructor.
* @param RequestStack $requestStack
* @param $default_lang
*/
public function __construct(RequestStack $requestStack, $default_lang)
{
$this->lang = $requestStack->getCurrentRequest()->get("lang", $default_lang);
}
/**
* @return mixed|string
*/
public function get()
{
return $this->lang;
}
}
public function myMethod(Lang $lang)
{
$value = $lang->get()
}