簡體   English   中英

如何在 Symfony 5 中獲取項目或根目錄

[英]How to get project or root dir in Symfony 5

我使用 Symfony 5 和日光浴室。 我想在我的 Controller(從 AbstractController 擴展)中獲取項目目錄(或根目錄)。

我看到了一些 kernel 的答案,創建新服務等。

但是,不只是一個 function 調用項目目錄而不創建一些自定義服務等嗎?

謝謝 !

您不需要為此提供任何服務。 您可以從 controller 中的容器中獲取。

$this->getParameter('kernel.project_dir');

其他方法是將其綁定在 yaml 上。

    _defaults:
        bind:
            # pass this value to any $projectDir argument for any service
            # that's created in this file (including controller arguments)
            string $projectDir: '%kernel.project_dir%'

因此,您可以將每個服務和 controller 注入為$projectDir

public class YourController {
   public function index(string $projectDir){
        // Your code
   }
}
public class YourService {
   public function __construct(string $projectDir){
        // Your code
   }
}

https://symfony.com/doc/current/configuration.html#accessing-configuration-parameters

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM