繁体   English   中英

Symfony4-无法自动连线服务

[英]Symfony4 - Can't Autowire Service

这是我第一次尝试在symfony4中自动连接服务,因为symfony4是新的,我不确定我在网上找到的anwser是否有效或已过时。

在我的services.yaml中:

services:

   [...]

    smugmug_controller:
        class: App\Controller\SmugmugController
        arguments:
            - '@smugmug_service'

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones
    smugmug_service:
        class: App\Services\SmugmugService
        arguments:
            $consumerKey: "%smugmug.consumer_key%"
            $consumerSecret: "%smugmug.consumer_secret%"
            $oauthToken: "%smugmug.oauth_token%"
            $oauthTokenSecret: "%smugmug.oauth_token_secret%"
            $allowedRootId: "%smugmug.allowed_root_id%"

在我的Smugmug服务中:

class SmugmugService 
{
    private $consumerKey;
    private $consumerSecret;
    private $oauthToken;
    private $oauthTokenSecret;
    private $allowedRootId;
    private $galleryNameFromDropbox = "dropbox";

    /**
     * Constructor.
     *
     * @param string $consumerKey
     * @param string $consumerSecret
     * @param string $oauthToken
     * @param string $oauthTokenSecret
     * @param string $allowedRootId
     */
    public function __construct(String $consumerKey, String $consumerSecret, String $oauthToken, String $oauthTokenSecret, String $allowedRootId) {
        $this->consumerKey = $consumerKey;
        $this->consumerSecret = $consumerSecret;
        $this->oauthToken = $oauthToken;
        $this->oauthTokenSecret = $oauthTokenSecret;
        $this->allowedRootId = $allowedRootId;
    }

在我的控制器中:

class SmugmugController extends Controller {

    private $smugmugService;

    public function __construct(SmugmugService $smugmugService) {
        $this->smugmugService = $smugmugService;
    }

当我尝试从控制器调用路由时,出现以下错误:

无法自动装配服务“ App \\ Services \\ SmugmugService”:方法“ __construct()”的参数“ $ consumerKey”为带类型提示的“字符串”,您应显式配置其值。

我知道我称其为具有注入服务的控制器,他本人已经注入了参数(这是问题吗?)。 有什么帮助吗?

@Cerad答案:

您想停止使用smugmug_controller之类的服务ID。 请使用完全限定的类名。 实际上,如果将ID替换为类名,则可以删除class属性。 每当您查看示例时,请始终确保它适用于带有自动装配的S4。 全部在文档中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM