在服务中使用 Symfonybundle 中的参数 class
Use parameters in Symfonybundle in service class
我有名为上传图片的 Symfony 包:
我想在我的 class.
中使用我的包中的参数
这是我的参数文件:
upload-images:
image:
crop_size: 300
我的文件:
Configuration.php
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('upload-images');
$treeBuilder->getRootNode()
->children()
->arrayNode('image')
->children()
->integerNode('save_original')->end()
->scalarNode('crop_size')->end()
->end()
->end() // twitter
->end();
return $treeBuilder;
}
}
UploadImagesExtension.php
class UploadImagesExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(dirname(__DIR__).'/Resources'));
$loader->load('services.yaml');
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
}
}
最后我的服务 class:
Rotate.php
并且在这个 class 中我想要参数:crop_size
我尝试了 ParameterBagInterface:
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class Rotate
{
private $params;
public function __construct(ParameterBagInterface $params)
{
$this->params = $params;
}
public function Rotate()
{
$cropSize = $params->get('crop_size');
}
}
UserController.php
use verzeilberg\UploadImagesBundle\Service\Rotate;
class UserController extends AbstractController
{
/** @var UserProfileService */
private $service;
private $userService;
public function __construct(
UserProfileService $service,
UserService $userService
) {
$this->service = $service;
$this->userService = $userService;
}
/**
* @param UserInterface $user
* @return Response
*/
public function profile(UserInterface $user)
{
$rotate = new Rotate();
$rotate->Rotate();
.....
...
}
Getting this error:
函数 verzeilberg\UploadImagesBundle\Service\Rotate::__construct() 的参数太少,在第 62 行 /home/vagrant/projects/diabetigraph-dev/src/Controller/User/UserController.php 中传递了 0 个参数,而恰好有 1 个预期
I have search for a solution. But did not came accross the right one.
您遇到的错误与您的问题没有直接关系。
这是自动装配问题,还是您正在尝试手动实例化服务?请在这里分享你在做什么:UserController.php on line 62
无论如何,回答你的问题:
要从参数包访问参数,您必须在扩展中设置它们。
$container->setParameter('my_bundle.config', $config);
此外,注入整个 ParameterBag 对于项目来说是好的,但对于 bundle 应该避免。
使用 DI 配置仅注入您的参数,或者使您的扩展实现 CompilerPassInterface
,并覆盖那里的定义。 (对于这样一个简单的任务来说可能有点过分了)
根据最新的编辑,错误很明显:如果你想使用依赖注入,你必须使用它。在没有任何构造函数参数的情况下调用 $rotate = new Rotate();
将失败,因为 Symfony 无法为您注入它们。
相反,通过以下操作注入它:
public function profile(UserInterface $user, Rotate $rotate)
...这将使用 Symfony 的容器并注入 ParameterBagInterface
,如果您启用了自动装配。如果没有,您必须编写适当的服务定义才能完成此操作
我有名为上传图片的 Symfony 包:
我想在我的 class.
中使用我的包中的参数这是我的参数文件:
upload-images:
image:
crop_size: 300
我的文件:
Configuration.php
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('upload-images');
$treeBuilder->getRootNode()
->children()
->arrayNode('image')
->children()
->integerNode('save_original')->end()
->scalarNode('crop_size')->end()
->end()
->end() // twitter
->end();
return $treeBuilder;
}
}
UploadImagesExtension.php
class UploadImagesExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(dirname(__DIR__).'/Resources'));
$loader->load('services.yaml');
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
}
}
最后我的服务 class:
Rotate.php
并且在这个 class 中我想要参数:crop_size
我尝试了 ParameterBagInterface:
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class Rotate
{
private $params;
public function __construct(ParameterBagInterface $params)
{
$this->params = $params;
}
public function Rotate()
{
$cropSize = $params->get('crop_size');
}
}
UserController.php
use verzeilberg\UploadImagesBundle\Service\Rotate;
class UserController extends AbstractController
{
/** @var UserProfileService */
private $service;
private $userService;
public function __construct(
UserProfileService $service,
UserService $userService
) {
$this->service = $service;
$this->userService = $userService;
}
/**
* @param UserInterface $user
* @return Response
*/
public function profile(UserInterface $user)
{
$rotate = new Rotate();
$rotate->Rotate();
.....
...
}
Getting this error:
函数 verzeilberg\UploadImagesBundle\Service\Rotate::__construct() 的参数太少,在第 62 行 /home/vagrant/projects/diabetigraph-dev/src/Controller/User/UserController.php 中传递了 0 个参数,而恰好有 1 个预期
I have search for a solution. But did not came accross the right one.
您遇到的错误与您的问题没有直接关系。
这是自动装配问题,还是您正在尝试手动实例化服务?请在这里分享你在做什么:UserController.php on line 62
无论如何,回答你的问题:
要从参数包访问参数,您必须在扩展中设置它们。
$container->setParameter('my_bundle.config', $config);
此外,注入整个 ParameterBag 对于项目来说是好的,但对于 bundle 应该避免。
使用 DI 配置仅注入您的参数,或者使您的扩展实现 CompilerPassInterface
,并覆盖那里的定义。 (对于这样一个简单的任务来说可能有点过分了)
根据最新的编辑,错误很明显:如果你想使用依赖注入,你必须使用它。在没有任何构造函数参数的情况下调用 $rotate = new Rotate();
将失败,因为 Symfony 无法为您注入它们。
相反,通过以下操作注入它:
public function profile(UserInterface $user, Rotate $rotate)
...这将使用 Symfony 的容器并注入 ParameterBagInterface
,如果您启用了自动装配。如果没有,您必须编写适当的服务定义才能完成此操作