【问题标题】:Secure a method, Fatal error, __construct() must implement interface保护方法,致命错误,__construct() 必须实现接口
【发布时间】:2018-07-06 17:33:36
【问题描述】:

我是 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

我真的不明白出了什么问题,任何帮助将不胜感激。

【问题讨论】:

  • 也许显示实现AuthorizationCheckerInterface???
  • 为什么投反对票?

标签: symfony symfony-2.8


【解决方案1】:

似乎您没有注入 AuthorizationCheckerInterface 的实例。

你在使用自动装配吗?您的服务是如何定义的?您是否尝试从容器中检索服务而不是注入它(尽管注入将是要走的路)? http://symfony.com/doc/2.8/service_container.html

【讨论】:

    【解决方案2】:

    似乎实现这一点的方法是添加

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

    而不是

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

    我确实用过

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

    【讨论】:

    • 你太难了。您已经在扩展基本控制器,因此您只需要 $this->isGranted('ROLE_ADMIN')。
    猜你喜欢
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多