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

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

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

問題描述

我希望在每個視圖中都可以使用有關(guān)系統(tǒng)區(qū)域設(shè)置的信息,以便我可以突出顯示用戶當前選擇的任何語言.經(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 變量,在視圖中訪問時,始終保持默認 系統(tǒng)區(qū)域設(shè)置,而不是當前 選擇的區(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)!

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

相關(guā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(響應(yīng)內(nèi)容必須是實現(xiàn) __toString()、“boolean和“boolean的字符串或?qū)ο?移動到 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())
主站蜘蛛池模板: 精品九九九 | 狠狠操狠狠 | 久久久国产精品视频 | 日韩av最新网址 | 一级视频在线免费观看 | 先锋资源吧| 九色在线观看 | 亚洲精品68久久久一区 | 午夜影院在线免费观看视频 | 99久久精品免费看国产高清 | 久久久男人的天堂 | 少妇性l交大片免费一 | 国产区在线观看 | 麻豆视频国产在线观看 | 日韩精品一区二区三区视频播放 | 色婷婷综合久久久中字幕精品久久 | 国产精品中文字幕在线 | 成人精品区 | 欧美999| 黄色精品 | 天天操夜夜操 | 亚洲成人精品 | 国产免费看 | 亚洲成人一二三 | 国产黄色网址在线观看 | 免费一级淫片aaa片毛片a级 | 欧美一区二区小视频 | 国产一区二区在线免费观看 | 精久久久 | 国产精品亚洲第一区在线暖暖韩国 | 1204国产成人精品视频 | 91偷拍精品一区二区三区 | 国产激情在线观看 | 夜夜爽99久久国产综合精品女不卡 | 黄色成人免费在线观看 | 欧美一区二区三区在线视频 | 四虎永久在线精品免费一区二 | 91免费版在线观看 | 久久久精品久久久 | 啪一啪在线视频 | 亚洲成av人片在线观看 |