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;
}
}
这篇关于如何在所有视图中共享变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在所有视图中共享变量?
基础教程推荐
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 使用 PDO 转义列名 2021-01-01