【发布时间】:2017-09-12 16:50:21
【问题描述】:
您好,我正在尝试创建一个与 riot API 通信的项目。我是 symfony 的新手,我发现了一个非常好的捆绑包,它启发了我,但在服务中,他注入了整个容器,我读到注入整个容器不是一个好主意,但我真的需要从 parameters.yml 中获取参数 我该怎么做?我也收到此错误:
Cannot autowire service "AppBundle\Controller\Service\ChampionService": argument "$container" of method "__construct()" references class "Symfony\Component\DependencyInjection\Container" but no such service exists. Try changing the type-hint to one of its parents: interface "Psr\Container\ContainerInterface", or interface "Symfony\Component\DependencyInjection\ContainerInterface".
我的服务:
namespace AppBundle\Controller\Service;
use AppBundle\Controller\guzzleClient\GuzzleClient;
use Psr\Log\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Container;
use AppBundle\Entity\Champion;
class ChampionService
{
/**
* @var GuzzleClient
*/
private $guzzle;
/**
* @var Container
*/
private $container;
/**
* Constructor
* @param GuzzleClient $guzzle
* @param Container $container
*/
public function __construct(GuzzleClient $guzzle, Container $container) {
$this->guzzle = $guzzle;
$this->container = $container;
}
/**
* Retrieves all the champions
*
* @param $region string
* @param $freeToPlay boolean
* @throws \Symfony\Component\CssSelector\Exception\InternalErrorException
* @return array
*/
public function getChampions($region, $freeToPlay = null) {
$request = $this->container->getParameter('roots')['champion']['champions'];
if($freeToPlay == null) {
$champions = $this->guzzle->send($request, $region);
} else {
$champions = $this->guzzle->send($request, $region, array('freeToPlay' => $freeToPlay));
}
return $this->createChampions($champions->champions);
}
/**
* Retrieves one champion
*
* @param integer $id the id of the champion
* @param $region string
* @throws \Symfony\Component\CssSelector\Exception\InternalErrorException
* @return \AppBundle\Entity\Champion
*/
public function getChampionById($id, $region) {
if(!is_int($id)) {
throw new InvalidArgumentException('The "id" must be an int');
}
$request = $this->container->getParameter('roots')['champion']['championById'];
$request = str_replace('{id}', $id, $request);
$champion = $this->guzzle->send($request, $region);
return $this->createChampion($champion);
}
/**
* Create an array of champions
*
* @param array $champions an array of json object chamions
* @return array
*/
private function createChampions($champions) {
$return = array();
foreach($champions as $champion) {
$return[] = $this->createChampion($champion);
}
return $return;
}
/**
* Create a champion
*
* @param $object \stdClass
* @return Champion
*/
private function createChampion($object) {
$champion = new Champion();
$champion->setActive($object->active);
$champion->setBotEnabled($object->botEnabled);
$champion->setBotMmEnabled($object->botMmEnabled);
$champion->setFreeToPlay($object->freeToPlay);
$champion->setId($object->id);
$champion->setRankedPlayEnabled($object->rankedPlayEnabled);
return $champion;
}
}
【问题讨论】:
-
Symfony 版本¿?
-
symfony 版本:3.3.8
-
检查我的答案.. 将 Container 更改为 ContainerInterface..
-
由于根是您需要的唯一参数,您应该查看文档以了解如何注入它。这将消除注入容器的需要。
标签: symfony containers