参数绑定
在 Symfony 3.4 或更高版本中,recommended approach 将使用 Autowiring 和 Argument Binding。允许将变量名称的声明“绑定”为配置文件中定义的所有服务的参数,而无需显式指定使用该参数的每个服务。
在定义命令服务和自动装配的同一配置文件中,将bind 选项添加到_defaults 规范,以及所需的参数变量名称。
app/config/services.yml
parameters:
PARAMETER_NAME: 'test'
services:
_defaults:
bind:
$PARAMETER_NAME: '%PARAMETER_NAME%'
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
AppBundle\:
resource: '../../src/AppBundle/*'
exclude: '../../src/AppBundle/{DependencyInjection,Entity,Tests}'
# Additional Services Below Here
之后,Symfony 会在将参数值指定为服务构造函数的参数时,自动将参数值传递给绑定的变量名。
src/AppBundle/Command/MyCommand.php
namespace AppBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class MyCommand extends Command
{
private $PARAMETER_NAME;
public function __construct($PARAMETER_NAME)
{
$this->PARAMETER_NAME = $PARAMETER_NAME;
parent::__construct();
}
// ...
public function execute(InputInterface $input, OutputInterface $output)
{
dump($this->PARAMETER_NAME);
exit;
}
public static function getDefaultName()
{
return 'app:my_command';
}
}
参数注入
另一种避免需要覆盖__construct 的方法是使用方法注入参数值,即使用calls 扩展服务定义。
app/config/services.yml
parameters:
PARAMETER_NAME: 'test'
services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
AppBundle\:
resource: '../../src/AppBundle/*'
exclude: '../../src/AppBundle/{DependencyInjection,Entity,Tests}'
AppBundle\Command\MyCommand:
calls:
- [setParameterName, ['%PARAMETER_NAME%']]
# Additional Services Below Here
src/AppBundle/Command/MyCommand.php
namespace AppBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class MyCommand extends Command
{
private $parameterName;
// ...
public function execute(InputInterface $input, OutputInterface $output)
{
dump($this->parameterName);
exit;
}
public function setParameterName($value)
{
$this->parameterName = $value;
}
}
或者,可以使用依赖注入将 Container 或 ParameterBag 注入到命令中,使其功能类似于控制器。
注入整个 Container 或 ParameterBag 是highly
discouraged。
只注入需要的参数和服务
改为。
在以下任一示例中,确保启用autowire 和autoconfigure。
app/config/services.yml
parameters:
PARAMETER_NAME: 'test'
services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
AppBundle\:
resource: '../../src/AppBundle/*'
exclude: '../../src/AppBundle/{DependencyInjection,Entity,Tests}'
# Additional Services Below Here
ContainerAwareCommand
(从 Symfony 4.2 开始贬值 - 在 Syfmony 5.0+ 中删除)
使用ContainerAwareCommand 与参数注入类似,但不是调用setParameterName()。启用autowiring 和autoconfig 后,Symfony 将使用从ContainerAwareInterface 实现的setContainer() 自动注入整个容器。
src/AppBundle/Command/MyCommand.php
namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class MyCommand extends ContainerAwareCommand
{
// ...
public function execute(InputInterface $input, OutputInterface $output)
{
dump($this->getContainer()->getParameter('PARAMETER_NAME'));
exit;
}
}
参数包注入
需要 Symfony 4.1+
要在启用autowire 时使用依赖注入注入ParameterBag,请将ParameterBagInterface 添加到服务构造函数中。
src/AppBundle/Command/MyCommand.php
namespace AppBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class MyCommand extends Command
{
private $parameterBag;
public function __construct(ParameterBagInterface $parameterBag)
{
$this->parameterBag = $parameterBag;
parent::__construct();
}
// ...
public function execute(InputInterface $input, OutputInterface $output)
{
dump($this->parameterBag->get('PARAMETER_NAME'));
exit;
}
public static function getDefaultName()
{
return 'app:my_command';
}
}