当前位置:网站首页>laravel下视图间共享数据
laravel下视图间共享数据
2022-06-24 13:03:00 【ignativs amor】
背景
最近在升级一个很老的项目,最后使用了laravel8处理项目,项目比较复杂,前端有三个登录注册的功能;所以登录以后,就需要传输登录的账号以及该账号有的权限到页面,这里就设计到了数据共享;
过程
方法一:使用控制器
定义三个基础控制器,在 __construct() 中获取需要的登录用户信息,然后需要该信息的模块继承对应的基础控制器(注:laravel执行顺序是先构造方法,再中间件)
方法二:试图门面share()方法实现
然后在boot()方法里添加两行进行测试:
View::share('name1', 'first name');
//也可使用
view()->share('name2','second name');
前端页面随便任何地方都可以加上 $name1 和 $name2
这样前端所有页面都可以显示出共享的数据,但是缺点是,在AppServiceProvider 这个函数中无法获取缓存,只能指向性的从数据库中查询一些定向数据或者自定义一些变量值,使用起来局限性比较大
方法三:视图门面composer() 方法实现
视图生成器是在呈现视图时调用的回调或类方法。如果希望每次呈现视图时将数据自动绑定到视图中,则视图生成器可以帮助你将这些逻辑组织到一起。如果应用程序中的多个路由或控制器返回同一个视图,并且始终需要特定的数据,那么视图生成器可能特别有用。
通常情况下,视图生成器在应用程序中的某个 服务提供者 里注册。
在本例中,我们将创建一个新的 \Providers\ViewShareServiceProvider来容纳本逻辑。
我们将使用 View facade 的 composer 方法来注册视图生成器。Laravel 没有存放视图生成器的默认目录,所以你可以随意地组织它们。举个例子,你可以创建一个 app/Http/View/Composers 目录来存放应用中的所有的视图生成器:
- 步骤1:
<?php
namespace App\Providers;
use App\Http\View\Composers\HrComposer;
use App\Http\View\Composers\UserComposer;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class ViewShareServiceProvider extends ServiceProvider
{
/** * 注册应用服务 * * @return void */
public function register()
{
//
}
/** * 引导应用服务 * * @return void */
public function boot()
{
// 使用基于类的生成器
View::composer('enterprise.*', HrComposer::class);//hr
View::composer(['pc.*','hunter.*'], UserComposer::class);//user
}
}
- 步骤2
如果你创建了新的一个服务提供者来存放你注册视图生成器的代码,那么你需要将这个服务提供器添加到配置文件 config/app.php 的 providers 数组中。 - 步骤3
注册了视图生成器,每次渲染视图的时候,都会执行 App\Http\View\Composers\HrComposer类的 compose 方法。那么下面我们来定义视图生成器的这个类
<?php
namespace App\Http\View\Composers;
use App\Services\HospitalService;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;
use Illuminate\View\View;
class HrComposer
{
public $user_permission_menu;
/** * 创建一个 视图生成器 * * @return void */
public function __construct()
{
// 依赖项由服务容器自动解析...
}
public function compose(View $view){
//获取登录的企业信息
$oHospital = '';
if (auth()->check()){
$this->user_permission_menu = Cache::get('user_power_'.auth()->user()->id);
$oHospital = HospitalService::getHospitalInfo(auth()->user()->id);
}
$current_route_name = Route::currentRouteName();
$view->with([
'power'=>$this->user_permission_menu,
'oHospital'=>$oHospital,
'current_route_name' => $current_route_name
]);
}
}
结果
这样,就可以在视图即将渲染时,执行试图生成器,将数据自动绑定到视图中,试图里,就可以获取你需要的数据喽
边栏推荐
- Harmony os. (2)
- 杰理之串口接收 IO 需要设置数字功能【篇】
- MIT-6.824-lab4A-2022(万字讲解-代码构建)
- 如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup
- English writing of Mathematics -- Basic Chinese and English vocabulary (common vocabulary of geometry and trigonometry)
- 杰理之.睡眠以后定时唤醒系统继续跑不复位【篇】
- 二造考生必看|巩固优选题库助力考生最后冲刺
- 如何避免下重复订单
- Jerrys timer0 uses the default pa13 to detect the pulse width [chapter]
- 文本对比学习综述
猜你喜欢
Baidu map API drawing points and tips
puzzle(016.2)指画星河
Rasa 3. X learning series - it is a great honor to be a source code contributor of Rasa contributors, and to build and share the rasa community with rasa source code contributors all over the world!
SAP Marketing Cloud 功能概述(三)
Autorf: learn the radiation field of 3D objects from single view (CVPR 2022)
Kunpeng arm server compilation and installation paddlepaddle
[deep learning] storage form of nchw, nhwc and chwn format data
v-if 和 v-show 的区别
如何在物联网低代码平台中进行任务管理?
一键生成大学、专业甚至录取概率,AI填报志愿卡这么神奇?
随机推荐
SAP Marketing Cloud 功能概述(三)
MySQL复合索引探究
pip uninstall all packages except builtin package
Method of establishing unity thermodynamic diagram
远程办公之:在家露营办公小工具| 社区征文
二叉树中最大路径和[处理好任意一颗子树,就处理好了整个树]
杰理之检测 MIC 能量自动录音自动播放参考【篇】
HarmonyOS.2
Operation of simulated examination platform for examination questions of coal production and operation units (safety production management personnel) in 2022
Jericho turns on shouting in all modes to increase mic automatic mute [chapter]
Kunpeng arm server compilation and installation paddlepaddle
JS remove string spaces
食品饮料行业渠道商管理系统解决方案:实现渠道数字化营销布局
[learn ZABBIX from scratch] I. Introduction and deployment of ZABBIX
专精特新“小巨人”再启动,“企业上云”数字赋能
如何避免下重复订单
谷歌WayMo提出R4D: 采用参考目标做远程距离估计
Grendao usage problems
杰理之开了多种解码格式插卡到播放等待的时间长【篇】
2022 construction elevator driver (construction special type of work) examination questions and online simulation examination