問題描述
我希望在每個視圖中都可以使用有關(guān)系統(tǒng)區(qū)域設(shè)置的信息,以便我可以突出顯示用戶當(dāng)前選擇的任何語言.經(jīng)過一番谷歌搜索后,我發(fā)現(xiàn)了 官方文檔.但是,在將代碼放入 boot()
之后像這樣:
I want information about the system locale to be available in every view, so I could highlight whatever language is currently selected by a user. After some googling around, I've found the value-sharing issue addressed in the official documentation. However, after putting the code into boot()
like this:
class AppServiceProvider extends ServiceProvider{
public function boot(){
view()->share('locale', Lang::getLocale());
}
}
$locale
變量,在視圖中訪問時,始終保持默認(rèn) 系統(tǒng)區(qū)域設(shè)置,而不是當(dāng)前 選擇的區(qū)域設(shè)置.為什么?
the $locale
variable, when accessed in views, always holds the default system locale, not the currently selected one. Why?
推薦答案
我通常使用 View Composers,因此它更清晰易讀.
I usually use View Composers so it's more clear and readable.
例如,如果我想與主導(dǎo)航欄共享一個變量到我的所有視圖,我遵循以下規(guī)則:
For example If I want to share a variable with the main navbar to all of my views I follow the below rules:
您可以使用 artisan cli 創(chuàng)建一個服務(wù)提供者:
You can create a service provider with artisan cli:
php artisan make:provider ViewComposerServiceProvider
在ViewComposerServiceProvider 文件中創(chuàng)建composeNavigation 方法,其中包含刀片模板main.nav-menu,它代表帶有共享變量的導(dǎo)航菜單.
In the ViewComposerServiceProvider file create composeNavigation method in which has the blade template main.nav-menu that represents the navmenu with shared variables.
ViewComposerServiceProvider 看起來像:
<?php
namespace AppProviders;
use IlluminateSupportServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->composeNavigation();
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
private function composeNavigation()
{
view()->composer('main.nav-menu', 'AppHttpViewComposersNavComposer');
}
}
2.創(chuàng)建作曲家
正如您在上面的文件中看到的,我們有 AppHttpViewComposersNavComposer.php,所以讓我們創(chuàng)建該文件.在 AppHttp 中創(chuàng)建文件夾 ViewComposers,然后在里面創(chuàng)建 NavComposer.php 文件.
2. Create Composer
As you saw in the file above we have AppHttpViewComposersNavComposer.php so let's create that file. Create the folder ViewComposers in the AppHttp and then inside create NavComposer.php file.
NavComposer.php 文件:
<?php
namespace AppHttpViewComposers;
use AppRepositoriesNavMenuRepository;
use IlluminateViewView;
class NavComposer
{
protected $menu;
public function __construct(NavMenuRepository $menu)
{
$this->menu = $menu;
}
public function compose(View $view)
{
$thing= $this->menu->thing();
$somethingElse = $this->menu->somethingElseForMyDatabase();
$view->with(compact('thing', 'somethingElse'));
}
}
3.創(chuàng)建倉庫
正如您在上面的 NavComposer.php 文件中看到的,我們有存儲庫.通常,我在 App 目錄中創(chuàng)建一個存儲庫,因此在 App 中創(chuàng)建 Repositories 目錄,然后在 NavMenuRepository.php 中創(chuàng)建 文件.
3. Create repository
As you saw above in the NavComposer.php file we have repository. Usually, I create a repository in the App directory, so create Repositories directory in the App and then, create inside NavMenuRepository.php file.
該文件是該設(shè)計模式的核心.在該文件中,我們必須獲取要與所有視圖共享的變量值.
This file is the heart of that design pattern. In that file we have to take the value of our variables that we want to share with all of our views.
看看下面的文件:
<?php
namespace AppRepositories;
use AppThing;
use DB;
class NavMenuRepository
{
public function thing()
{
$getVarForShareWithAllViews = Thing::where('name','something')->firstOrFail();
return $getVarForShareWithAllViews;
}
public function somethingElseForMyDatabase()
{
$getSomethingToMyViews = DB::table('table')->select('name', 'something')->get();
return $getSomethingToMyViews;
}
}
這篇關(guān)于如何在所有視圖中共享變量?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!