如何在所有视图之间共享变量? [英] How to share a variable across all views?

查看:79
本文介绍了如何在所有视图之间共享变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有关系统区域设置的信息在每个视图中都可用,因此我可以突出显示用户当前选择的任何语言.经过一番探索,我发现了

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变量时,它始终保留 default 系统语言环境,而不是当前所选择的语言环境.为什么?

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.

例如,如果要与主导航栏共享一个变量给所有视图,请遵循以下规则:

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创建服务提供商:

You can create a service provider with artisan cli:

php artisan make:provider ViewComposerServiceProvider

ViewComposerServiceProvider 文件中,创建 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 App\Providers;

use Illuminate\Support\ServiceProvider;

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', 'App\Http\ViewComposers\NavComposer');
    }
}

2.创建作曲家

如您在上面的文件中看到的,我们有 App \ Http \ ViewComposers \ NavComposer.php ,所以让我们创建该文件.在 App \ Http 中创建文件夹 ViewComposers ,然后在内部创建 NavComposer.php 文件.

2. Create Composer

As you saw in the file above we have App\Http\ViewComposers\NavComposer.php so let's create that file. Create the folder ViewComposers in the App\Http and then inside create NavComposer.php file.

NavComposer.php 文件:

<?php

namespace App\Http\ViewComposers;

use App\Repositories\NavMenuRepository;
use Illuminate\View\View;

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.创建存储库

如您在上面的 NavComposer.php 文件中所看到的,我们有存储库.通常,我在 App 目录中创建一个存储库,因此在 App 中创建 Repositories 目录,然后在 NavMenuRepository.php中创建文件.

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 App\Repositories;

use App\Thing;
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;
    }

}

这篇关于如何在所有视图之间共享变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆