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

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

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

問題描述

我希望在每個視圖中都可以使用有關(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)!

【網(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())
主站蜘蛛池模板: 国产在线中文字幕 | 国内精品视频免费观看 | 午夜视频免费在线观看 | 成人亚洲网站 | 国产乱码精品一区二区三区忘忧草 | 精品视频在线播放 | 精品日韩一区二区 | 成人黄色在线视频 | 亚洲国产中文字幕 | 福利久久 | 伦理午夜电影免费观看 | 午夜视频一区二区三区 | 粉嫩粉嫩芽的虎白女18在线视频 | 天天操综合网站 | 波多野结衣二区 | 国产免费播放视频 | 日韩视频中文字幕 | 二区中文字幕 | 日韩在线三级 | 日韩综合在线 | 国产不卡视频 | h视频在线观看免费 | 中文字幕 亚洲一区 | 欧美一区二区三区在线播放 | 黄色毛片大全 | 欧美a级网站 | 亚洲精品www. | 国产精品片aa在线观看 | 日韩av中文 | 成人午夜在线 | 亚洲网站在线观看 | 亚洲电影一区 | 99精品视频免费观看 | 9999国产精品欧美久久久久久 | 性一交一乱一伦视频免费观看 | 一区二区三区播放 | 免费观看www7722午夜电影 | 国产欧美日韩一区二区三区在线观看 | 野狼在线社区2017入口 | 在线观看日韩av | 国产精品一区二区三区在线 |