![](/img/trans.png)
[英]How to solve “Unable to create the ”%kernel.root_dir%/../web/uploads“ directory” error in Symfony2
[英]Inject kernel.root_dir to entity constructor in Symfony2
我搜索並閱讀了很多相同的問題,但我得到了同樣的錯誤:/
我創建了一個服務:
parameters:
tbackend.report.class: T\BackendBundle\Entity\Report
services:
tbackend.entity.report:
class: %tbackend.report.class%
arguments: ["%kernel.root_dir%"]
我在T \\ BackendBundle \\ Entity \\ Report中有這個:
public function __construct($rootDir){
$this->rootDir = $rootDir;
}
當我嘗試創建新的Report(); 我收到這個消息:
警告:缺少T \\ BackendBundle \\ Entity \\ Report :: __ construct()的參數1,在第62行的/var/www/test/t_study/src/T/BackendBundle/Entity/ReportRepository.php中調用並定義
注意事項:我知道services.yml被調用,我在這個文件中有更多服務,所有工作正常(Loginhandlers等),我只添加一個(tbackend.entity.report)
這有什么問題? :(我不知道是否需要更多了解問題。我按照symfony2服務容器指南http://symfony.com/doc/master/book/service_container.html
基本上我在移動文件時盡量不在實體中使用DIR
泰
實例化類時,使用普通的PHP。 Symfony並不是一種魔術,可以在PHP的實例化過程中自動注入構造函數中的東西。
如果您想獲得服務,您必須在您需要的類中注入服務,或者您擁有容器(例如,在控制器中)並從容器中檢索服務。
$report = $this->container->get('tbackend.entity.report');
或:(除控制器外,在所有情況下都是更好的做法)
class WhereINeedTheReport
{
private $report;
public function __construct(Report $report)
{
$this->report = $report;
}
}
services:
# ...
where_i_need_the_report:
class: ...
arguments: ["@tbackend.entity.report"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.