2017-06-23 34 views
0

我只是在symfony中創建了一個新的服務:您已請求不存在的服務「file.uploader」 - Symfony3

// AppBundle\Services\FileUploader.php 

<?php 

namespace AppBundle\Services; 

class FileUploader { 
    private $imagesDir; 
    private $videosDir; 

    public function __construct($imagesDir, $videosDir) { 
     $this->imagesDir = $imagesDir; 
     $this->videosDir = $videosDir; 
    } 

    public function upload($form) { 
    ///... 
    ///... 
     } 
     return $form; 
    } 
} 

我還添加了我的services.yml文件中的以下內容:

// app\config\services.yml 

file.uploader: 
    class: AppBundle\Services\FileUploader 
    arguments: 
     - '%images_directory%' 
     - '%videos_directory%' 

但後來,當我嘗試從我的控制器調用它使用:

$uploadModel = $this->get('file.uploader'); 

我得到以下錯誤:

您已請求不存在的服務「file.uploader」。

即使我在config.yml和運行

php bin/console debug:container file.upload --show-arguments 

已經指定了我的論點,我得到:

Service ID  file.upload 
Class   AppBundle\Services\FileUploader 

任何想法

回答

0

你應該標記服務爲public

file.uploader: 
    class: AppBundle\Services\FileUploader 
    public: true 
    arguments: 
     - '%images_directory%' 
     - '%videos_directory%' 
0

服務通常保存在緩存中。 你有沒有試過清潔它?

php app/console cache:clear --env=prod 
php app/console cache:clear --env=dev 
+0

是的,我做到了,它仍然沒有工作 –

+0

你有Xdebug的設置?我認爲這將是解決這個問題的最快方法。只需執行代碼並查看爲什麼找不到服務。 – matiit

+0

我一定會在下次再檢查一次,謝謝。 –

相關問題