1 回答

TA貢獻1817條經驗 獲得超14個贊
您描述的參數可以在定義為例如的配置中使用;
parameters:
the_answer: 42
然后,您可以在進一步的配置中使用這些值(例如,請參見下文)?;蛘撸绻朐诳刂破髦刑幚磉@些值,您可以(不再推薦)使用$this->getParameter('the_answer')來獲取值。
綁定參數(推薦):
這種方法將綁定值,然后您可以通過引用參數在控制器函數/服務中獲?。ㄗ詣由衿娴刈⑷耄┻@些值。
這些值的范圍可以從簡單的標量值到服務、.env 變量、php 常量以及配置可以解析的所有其他內容。
# config/services.yaml
services:
_defaults:
bind:
string $helloWorld: 'Hello world!' # simple string
int $theAnswer: '%the_answer%' # reference an already defined parameter.
string $apiKey: '%env(REMOTE_API)%' # env variable.
然后當我們做類似的事情時,這些會被注入到服務/控制器函數中:
public function hello(string $apiKey, int $theAnswer, string $helloWorld) {
// do things with $apiKey, $theAnswer and $helloWorld
}
可以在 symfony 文檔中找到更多詳細信息和示例https://symfony.com/doc/current/service_container.html#binding-arguments-by-name-or-type
注入服務(替代)
您也可以使用 arguments 將其直接注入到定義的服務中。
# config/services.yaml
services:
# explicitly configure the service
App\Updates\SiteUpdateManager:
arguments:
$adminEmail: '[email protected]'
- 1 回答
- 0 關注
- 123 瀏覽
添加回答
舉報