2017-07-19 57 views
0

我不明白爲什麼我無法創建自定義服務。我遇到了這兩種技術的錯誤。我在文檔中找不到任何關於此的信息。添加自定義服務 - Symfony; Sylius

enter image description here

# app/config/services.yml 
services: 

    jdf.utils.phphelper: 
     class: JDF\Utils\PhpHelper 



// src/JDF/Utils/PhpHelper.php 

namespace JDF\Utils; 

class PhpHelper 
{ 

    /** 
    * [pdebug description] 
    * @param string $var   The string to beautiful show 
    * @param string $msg   Description of the $var 
    * @param integer $displayNone 
    * @return echo pre print_r $var string 
    */ 
    public function pdebug ($var, $msg = '', $displayNone = 0) { 
    } 

} 

情況1:(通行證PhpHelper在__construct函數)

// src/JDF/CsvTreatmentBundle\Controller/ImportController 

namespace JDF\CsvTreatmentBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Response; 

use JDF\Utils\PhpHelper; 
use Psr\Log\LoggerInterface; 

/** 
* 
*/ 
class ImportController extends Controller { 

    function __construct(
          PhpHelper $PhpHelper 
         ) { 
    } 

    public function indexAction() { 
     //$test = $this->container->get('jdf.utils.phphelper'); 
     return new Response('<hr>'); 
    } 

} /*End of class*/ 

錯誤1: 捕致命錯誤:傳遞給JDF參數1 \ CsvTreatmentBundle \ Controller \ ImportController :: __ construct()必須是JDF \ Utils \ PhpHelper的一個實例,沒有給出,在C:\ kitutilitaire \ vendor中調用\ symfony的\ symfony的\ SRC \的Symfony \元器件\ HttpKernel \上線202控制器\ ControllerResolver.php和定義 500內部服務器錯誤 - ContextErrorException

案例2(只是使用get()控制器的方法):

// src/JDF/CsvTreatmentBundle\Controller/ImportController 

    namespace JDF\CsvTreatmentBundle\Controller; 

    use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
    use Symfony\Component\HttpFoundation\Response; 

    use JDF\Utils\PhpHelper; 
    use Psr\Log\LoggerInterface; 

    /** 
    * 
    */ 
    class ImportController extends Controller { 

     function __construct(
           //PhpHelper $PhpHelper 
           // LoggerInterface $logger 
          ) { 
     } 

     public function indexAction() { 

      $test = $this->container->get('jdf.utils.phphelper'); 
      // $logger = $this->container->get('logger'); 

      return new Response('<hr>'); 
     } 

    } /*End of class*/ 

錯誤2: 已嘗試從名稱空間 「JDF \ utils的」 加載類 「PhpHelper」。 你忘記了另一個命名空間的「使用」語句嗎?

堆棧跟蹤

in var\cache\dev\appDevDebugProjectContainer.php at line 3555 - 
     */ 
     protected function getJdf_Utils_PhphelperService() 
     { 
      return $this->services['jdf.utils.phphelper'] = new \JDF\Utils\PhpHelper(); 
     } 
     /** 

編輯:composer.json自動加載

"autoload": { 
    "psr-4": { 
     "AppBundle\\": "src/AppBundle/", 
     "JDF\\CsvTreatmentBundle\\": "src/JDF/CsvTreatmentBundle/", 
     "JDF\\Utils\\": "src/JDF/Utils/PhpHelper" 
    }, 
    "classmap": ["app/AppKernel.php", "app/AppCache.php"] 
}, 

感謝前請先對您有所幫助。

回答

0

默認情況下,控制器不會獲得任何注入。他們有$this->container始終可以得到您的所有服務。

所以沒有更多的做多:

class ImportController extends Controller { 
    public function indexAction() { 
     $test = $this->container->get('jdf.utils.phphelper'); 
     // $logger = $this->container->get('logger'); 

     return new Response('<hr>'); 
    } 

} 

FYI:緩存文件appDevDebugProjectContainer是自動生成的,並沒有任何意義的問題。

+0

如何在services.yml中定義服務?我寫了我的services.yml(第一個代碼展示) – Xenofexs

+0

@Xenofexs:你說得對,我的不好。我已經更新了答案。 – colburton

+0

感謝您的更新,但這是我的情況2(已編輯,我已刪除非浮動行) 使用此代碼,我得到了error2(嘗試從命名空間「JDF \ Utils」加載類「PhpHelper」 ) 和行錯誤:'return $ this-> services ['jdf.utils.phphelper'] = new \ JDF \ Utils \ PhpHelper();' – Xenofexs

0

我已經解決了改變我的composer.json的問題。

對於可以使用$this->container->get('jdf.utils.phphelper');所有優秀的代碼是:

# app/config/services.yml 
services: 

    jdf.utils.phphelper: 
     class: JDF\Utils\PhpHelper 
// src/JDF/Utils/PhpHelper.php 

namespace JDF\Utils; 

class PhpHelper {} 

// src/JDF/CsvTreatmentBundle\Controller/ImportController 

namespace JDF\CsvTreatmentBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 

use JDF\Utils\PhpHelper; 

/** 
* 
*/ 
class ImportController extends Controller { 

    public function indexAction() { 

     $test = $this->container->get('jdf.utils.phphelper'); 

     return new Response('<hr>'); 
    } 

} /*End of class*/ 

和重要信息:composer.json:

"autoload": { 
    "psr-4": { 
     "JDF\\CsvTreatmentBundle\\": "src/JDF/CsvTreatmentBundle/", 
     "JDF\\Utils\\": "src/JDF/Utils/" 
    }, 
    "classmap": ["app/AppKernel.php", "app/AppCache.php"] 
}, 

和CLI命令:php composer.phar dump-autoload

感謝到colburton爲這次和我的問題感興趣。