安全方法,致命错误,__ construct()必须实现接口

时间:2018-07-06 17:33:37

标签: symfony symfony-2.8

我对Symfony还是很陌生,所以很抱歉如果我问一个新手问题,但是运行了生产服务,现在一切都掉下来了,所以请尝试解决此问题。

我需要为代码的特定部分添加安全性,并且这样做

我添加了

if (true === $this->authorizationChecker->isGranted('ROLE_ADMIN'))
在我的代码中,按照下面提供的文档进行操作:https://symfony.com/doc/2.8/security/securing_services.html

我的整个代码看起来像:

<?php

namespace SUP\SupervisorBundle\Controller;

use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
...

class AutoCompleteController extends Controller
{
    protected $authorizationChecker;

    public function __construct(AuthorizationCheckerInterface $authorizationChecker)
    {
        $this->authorizationChecker = $authorizationChecker;
    }

但是由于未知原因,我得到了Catchable Fatal Error: Argument 1 passed to SUP\SupervisorBundle\Controller\AutoCompleteController::__construct() must implement interface Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface, none given, called in sup/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php on line 186 and defined

我真的不明白问题出在哪里,任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

似乎您没有注入AuthorizationCheckerInterface实例。

您正在使用自动装配吗?如何定义您的服务?您是否尝试过从容器中检索服务而不是注入服务(尽管注入是一种方法)? http://symfony.com/doc/2.8/service_container.html

答案 1 :(得分:0)

看来实现此目标的方法是添加

protected $authorizationChecker;

    public function __construct(AuthorizationCheckerInterface $authorizationChecker)
    {
        $this->authorizationChecker = $authorizationChecker;
    }

,而不是

$this->authorizationChecker->isGranted('ROLE_ADMIN')

我确实使用过

$this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')