久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在所有視圖中共享變量?

How to share a variable across all views?(如何在所有視圖中共享變量?)
本文介紹了如何在所有視圖中共享變量?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我希望在每個視圖中都可以使用有關系統(tǒng)區(qū)域設置的信息,以便我可以突出顯示用戶當前選擇的任何語言.經過一番谷歌搜索后,我發(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 變量,在視圖中訪問時,始終保持默認 系統(tǒng)區(qū)域設置,而不是當前 選擇的區(qū)域設置.為什么?

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.

例如,如果我想與主導航欄共享一個變量到我的所有視圖,我遵循以下規(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)建一個服務提供者:

You can create a service provider with artisan cli:

php artisan make:provider ViewComposerServiceProvider

ViewComposerServiceProvider 文件中創(chuàng)建composeNavigation 方法,其中包含刀片模板main.nav-menu,它代表帶有共享變量的導航菜單.

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.

該文件是該設計模式的核心.在該文件中,我們必須獲取要與所有視圖共享的變量值.

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;
    }

}

這篇關于如何在所有視圖中共享變量?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Laravel Eloquent Union query(Laravel Eloquent Union 查詢)
Overwrite laravel 5 helper function(覆蓋 Laravel 5 輔助函數(shù))
laravel querybuilder how to use like in wherein function(laravel querybuilder 如何在 where 函數(shù)中使用 like)
The Response content must be a string or object implementing __toString(), quot;booleanquot; given after move to psql(響應內容必須是實現(xiàn) __toString()、“boolean和“boolean的字符串或對象.移動到 psql 后給出) - IT屋-程
Roles with laravel 5, how to allow only admin access to some root(Laravel 5 的角色,如何只允許管理員訪問某些根)
Laravel Auth - use md5 instead of the integrated Hash::make()(Laravel Auth - 使用 md5 而不是集成的 Hash::make())
主站蜘蛛池模板: 久草热8精品视频在线观看 午夜伦4480yy私人影院 | 911精品美国片911久久久 | 成人免费视频网站在线看 | 99re热精品视频 | 亚洲欧洲日韩 | 91久久精品日日躁夜夜躁国产 | 先锋影音资源网站 | 亚洲一区免费 | 9191在线播放 | 日本三级线观看 视频 | 午夜影院网站 | 五月天婷婷丁香 | 欧美精品一区二区三区视频 | 日本免费在线 | 国内精品视频免费观看 | 一区二区视频 | 9久久婷婷国产综合精品性色 | 日韩欧美在线视频 | 日韩人体视频 | 国产精品久久 | 精品免费在线 | 亚洲444kkkk在线观看最新 | 在线免费看黄 | 91毛片网| 中国一级特黄真人毛片免费观看 | 秋霞在线一区 | 一区二区三区免费 | 九色91视频 | 中文字幕一区在线观看视频 | 久久久久久久一区 | 精品国产综合 | 国产黄色大片在线观看 | 97国产精品视频人人做人人爱 | 欧美一区二区三区久久精品视 | 麻豆av片| 国产成人啪免费观看软件 | 精品亚洲永久免费精品 | 一区二区三区视频在线 | 国产不卡在线 | 久草欧美视频 | 九九热这里 |