[英]Laravel setting from database and accessible to all the Controllers and Views
嗨,我正在嘗試加載所有控制器和視圖的設置和可訪問性。 有沒有一個很好的方法來做到這一點。
正如我所提到的,您可以修改所有其他控制器擴展的控制器,並將變量添加到__construct()函數中。 例如,在我的5.3項目中,所有控制器都擴展了App \\ Http \\ Controllers \\ Controller。 在該控制器中,添加(或更新,如果函數存在),如下所示:
protected $settings; // Protected so child controllers can't modify it
public function __construct() {
$this->settings = Settings::first();
}
你可以在這里做各種邏輯並設置你想要的任何變量。 然后在子控制器中,只需使用$this->settings
,就可以訪問它們
if($this->settings->my_setting == TRUE)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.