symfony从parameters.yml获取参数

时间:2017-09-12 16:50:22

标签: symfony containers

您好我试图创建一个与riot API通信的项目。我是symfony的新手,我发现了一个非常好的捆绑,这启发了我,但在服务中他注入了整个容器,我读到注入整个容器不是一个好主意,但我真的需要来自parameters.yml的getParameter 我怎么能这样做?我也得到这个错误:

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;
        }
    }

2 个答案:

答案 0 :(得分:0)

其他解决方案,在service.yml

services:
    your_service:
        class:  Your\Bundle\Service\YourService
        arguments: [%param1%, %param2%]

在课堂上

public function __construct($param1, $param2){}

但是你在服务中使用容器,这是正常的。

更改

Symfony\Component\DependencyInjection\Container;

Symfony\Component\DependencyInjection\ContainerInterface as Container;

我在Symfony项目中的示例 V3.2 ContainerInterface

use Symfony\Component\DependencyInjection\ContainerInterface;

class YourClassService {

/**
 * @var ContainerInterface
 */
protected $serviceContainer;


/**
 * @param ContainerInterface $serviceContainer
 */
public function __construct(ContainerInterface $serviceContainer)
{
   $this->serviceContainer = $serviceContainer;

   $param = $this->serviceContainer->getParameter('parameter');
}

}

在service.yml

services:
    your.class.service.api:
       class: YourClassService
       arguments: ["@service_container"]

答案 1 :(得分:-1)

Symfony3

 $this->getParameter('yourParameter');