• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在所有视图共享变量?

用户头像
it1352
帮助1

问题说明

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

正确答案

#1

我通常使用 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;
    }

}

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tangibjbf
系列文章
更多 icon
同类精品
更多 icon
继续加载