首页 文章

Symfony4 - 无法自动服务

提问于
浏览
0

这是我第一次尝试在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”是类型提示“字符串”,您应该明确配置其值 .

我知道我用一个注入的服务调用一个控制器,他自己注入了参数(这是问题吗?) . 有帮助吗?

1 回答

  • 1

    @Cerad回答:

    您想要停止使用像smugmug_controller这样的服务ID . 请改用完全限定的类名 . 实际上,如果用类名替换id,则可以删除class属性 . 无论何时你看一个例子,总是确保它是用于带有自动装配的S4 . 一切都在文档中 .

相关问题