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

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

}

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)