当前位置:网站首页>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
]);
}
}
结果
这样,就可以在视图即将渲染时,执行试图生成器,将数据自动绑定到视图中,试图里,就可以获取你需要的数据喽
边栏推荐
- 如何避免下重复订单
- 2022 recurrent training question bank and answers for hoisting signal Rigger (special type of construction work)
- 2022 coal mine gas drainage operation certificate examination questions and simulation examination
- Home office should be more efficient - automated office perfectly improves fishing time | community essay solicitation
- Solution of channel management system for food and beverage industry: realize channel digital marketing layout
- MES在流程和离散制造企业的15个差别(下)
- Development of B2B transaction collaborative management platform for kitchen and bathroom electrical appliance industry and optimization of enterprise inventory structure
- Jupiter notebook operation
- Kunpeng arm server compilation and installation paddlepaddle
- Autorf: learn the radiation field of 3D objects from single view (CVPR 2022)
猜你喜欢

Telecommuting: camping at home office gadgets | community essay solicitation

文本对比学习综述

Development of B2B transaction collaborative management platform for kitchen and bathroom electrical appliance industry and optimization of enterprise inventory structure

数商云:加强供应商管理,助推航空运输企业与供应商高效协同

P2pdb white paper

#21Set经典案例
![NPM package [details] (including NPM package development, release, installation, update, search, uninstall, view, version number update rules, package.json details, etc.)](/img/b0/85ac6274b239e42c9543fa296df456.png)
NPM package [details] (including NPM package development, release, installation, update, search, uninstall, view, version number update rules, package.json details, etc.)

Research on MySQL composite index

Getting to know cloud native security for the first time: the best guarantee in the cloud Era

P2PDB 白皮书
随机推荐
【无标题】
Method of inputting dots under letters in markdown/latex
杰理之TIMER0 用默认的 PA13 来检测脉宽【篇】
unity 等高线创建方法
钛星数安加入龙蜥社区,共同打造网络安全生态
IDEA连接mysql自定义生成实体类代码
【环境搭建】zip 分卷压缩
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!
Rongyun communication has "hacked" into the heart of the bank
专精特新“小巨人”再启动,“企业上云”数字赋能
v-for 中 key的作用和原理
数学之英文写作——基本中英文词汇(几何与三角的常用词汇)
Three efficient programming skills of go language
【LeetCode】10、正则表达式匹配
Google waymo proposed r4d: remote distance estimation using reference target
v-if 和 v-show 的区别
21set classic case
Research on MySQL composite index
Defoaming
Development of B2B transaction collaborative management platform for kitchen and bathroom electrical appliance industry and optimization of enterprise inventory structure