Service Symfony 4-私人外部服务的最佳实践

Service Symfony 4-私人外部服务的最佳实践,service,external,private,public,symfony4,Service,External,Private,Public,Symfony4,我已经安装了Symfony 4的最新版本,它是rocks 但我有一个问题,当我们在您的控制器中使用外部专用服务时,更好的方法是什么: 例如,我有一个私人的jwt服务经理;我无法在控制器中直接调用此服务,因为我遇到以下错误: The "lexik_jwt_authentication.jwt_manager" service or alias has been removed or inlined when the container was compiled. You should either

我已经安装了Symfony 4的最新版本,它是rocks

但我有一个问题,当我们在您的控制器中使用外部专用服务时,更好的方法是什么:

例如,我有一个私人的jwt服务经理;我无法在控制器中直接调用此服务,因为我遇到以下错误:

The "lexik_jwt_authentication.jwt_manager" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead."
解决方案1:

    public function myAction(JWTTokenManagerInterface $jwt) {
    // $jwt->...   
}
我创建了一个公共JWTService,如下所示:

<?php
namespace App\Service\JWT;

use FOS\UserBundle\Model\UserInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;

/**
 * Class JwtService
 * @package App\Service\JWT
 */
class JwtService
{
    /**
     * @var $JwtManager
     */
    private $JwtManager;

    public function __construct(JWTTokenManagerInterface $JwtManager)
    {
        $this->JwtManager = $JwtManager;
    }

    /**
     * @param UserInterface $user
     * @return string
     */
    public function create(UserInterface $user)
    {
        return $this->JwtManager->create($user);
    }
} 
在我的控制器中,我使用这样的服务

class UserController extends Controller {

  private $jwt;

  public function __construct(JWTTokenManagerInterface $jwt) {
    $this->jwt = $jwt;
  }

  public function myAction() {
    // $this->jwt->...
  }
}

提前感谢。

注射(2个选项)。自动布线可以解决这个问题。尽量避免接触容器。

解决方案3:

    public function myAction(JWTTokenManagerInterface $jwt) {
    // $jwt->...   
}
@乔治,你觉得这是个好办法吗


Thx.

我在这里也有同样的问题,谢谢这是我的想法,强迫我去做不太大的控制器;)可能适用于某些用例,但我将尽可能避免这种情况