如何在所有视图中共享变量?

How to share a variable across all views?(如何在所有视图中共享变量?)
本文介绍了如何在所有视图中共享变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我希望在每个视图中都可以使用有关系统区域设置的信息,以便我可以突出显示用户当前选择的任何语言.经过一番谷歌搜索后,我发现了 官方文档.但是,在将代码放入 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 变量,在视图中访问时,始终保持默认 系统区域设置,而不是当前 选择的区域设置.为什么?

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 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.创建作曲家

正如您在上面的文件中看到的,我们有 AppHttpViewComposersNavComposer.php,所以让我们创建该文件.在 AppHttp 中创建文件夹 ViewComposers,然后在里面创建 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.创建仓库

正如您在上面的 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 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;
    }

}

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

In PHP how can you clear a WSDL cache?(在 PHP 中如何清除 WSDL 缓存?)
failed to open stream: HTTP wrapper does not support writeable connections(无法打开流:HTTP 包装器不支持可写连接)
Stop caching for PHP 5.5.3 in MAMP(在 MAMP 中停止缓存 PHP 5.5.3)
Caching HTTP responses when they are dynamically created by PHP(缓存由 PHP 动态创建的 HTTP 响应)
Memcached vs APC which one should I choose?(Memcached 与 APC 我应该选择哪一个?)
What is causing quot;Unable to allocate memory for poolquot; in PHP?(是什么导致“无法为池分配内存?在 PHP 中?)