2 Symfony 3项目中的弃用

时间:2017-09-15 01:43:52

标签: symfony

我尝试在service.yml中定义一个自定义服务时,我的Symfony 3项目中有两个弃用,我想解决这个问题,但我不能解决这个问题......

enter image description here

这是我的代码FollowingExtension.php

 <?php
namespace AppBundle\Twig;

use Symfony\Bridge\Doctrine\RegistryInterface;

class FollowingExtension extends \Twig_Extension {
    protected $doctrine;

    public function __construct(RegistryInterface $doctrine) {
        $this->doctrine = $doctrine;
    }

    public function getFilters() {
        return array(
            new \Twig_SimpleFilter('following', array($this, 'followingFilter'))
        );
    }

    public function followingFilter($user, $followed){
        $following_repo = $this->doctrine->getRepository('BackendBundle:Following');
        $user_following = $following_repo->findOneBy(array(
            "user" => $user,
            "followed" => $followed
        ));

        if(!empty($user_following) && is_object($user_following)){
            $result = true;
        }else{
            $result = false;
        }

        return $result;
    }

    public function getName() {
        return 'following_extension';
    }
}

这是我的services.yml

following.twig_extension:
    class: AppBundle\Twig\FollowingExtension
    public: false
    arguments:
        $doctrine: "@doctrine"
    tags:
        - { name: twig.extension }

我很感激他们在试图解决我的问题时给予的帮助。

2 个答案:

答案 0 :(得分:0)

第一个问题解决方案

看起来似乎是因为在您的班级中声明了重复名称的服务:

`return 'following_extension';`

确保您只有一个名为following_extension的树枝服务。如果您确定只有一个名为following_extension的twig服务,您可能会使用该类注册多个服务。

第二个问题解决方案

替换

`use Symfony\Bridge\Doctrine\RegistryInterface;`

`use Doctrine\Common\Persistence\ManagerInterface;`

并替换

`public function __construct(RegistryInterface $doctrine) {`

`public function __construct(ManagerInterface $doctrine) {`

答案 1 :(得分:0)

终于解决了问题,我希望分享信息......

阅读Symfony的文档发现了这个How to Create Service Aliases and Mark Services as Private,我以这种方式宣布了我的服务:

service.yml:

中的

following.twig_extension: '@AppBundle\Twig\FollowingExtension'

FollowingExtension.php

    <?php
namespace AppBundle\Twig;

use Doctrine\Common\Persistence\ManagerRegistry;

class FollowingExtension extends \Twig_Extension {
    private $managerRegistry;

    public function __construct(ManagerRegistry $managerRegistry) {
        $this->managerRegistry = $managerRegistry;
    }

    public function getFilters() {
        return array(
            new \Twig_SimpleFilter('following', array($this, 'followingFilter'))
        );
    }

    public function followingFilter($user, $followed){
        $following_repo = $this->managerRegistry->getRepository('BackendBundle:Following');
        $user_following = $following_repo->findOneBy(array(
            "user" => $user,
            "followed" => $followed
        ));

        if(!empty($user_following) && is_object($user_following)){
            $result = true;
        }else{
            $result = false;
        }

        return $result;
    }

}

感谢您帮助我,如果我的英语不好,我很抱歉。