当前位置:网站首页>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 coal mine gas drainage operation certificate examination questions and simulation examination
- pip uninstall all packages except builtin package
- Generate binary tree according to preorder & inorder traversal [partition / generation / splicing of left subtree | root | right subtree]
- Jerry's infrared filtering [chapter]
- 遠程辦公之:在家露營辦公小工具| 社區征文
- 一键生成大学、专业甚至录取概率,AI填报志愿卡这么神奇?
- 10_那些格调很高的个性签名
- 如何避免下重复订单
- 杰理之可能出现有些芯片音乐播放速度快【篇】
- 90% of the project managers have skipped the pit. Are you still in the pit?
猜你喜欢
![Jerry's infrared filtering [chapter]](/img/6b/7c4b52d39a4c90f969674a5c21b2c7.png)
Jerry's infrared filtering [chapter]

Convolution kernel and characteristic graph visualization

SAP Marketing Cloud 功能概述(四)

Home office should be more efficient - automated office perfectly improves fishing time | community essay solicitation

SaaS management system solution of smart Park: enabling the park to realize information and digital management

2022 Quality Officer - Equipment direction - post skills (Quality Officer) recurrent training question bank and online simulation examination

Rongyun communication has "hacked" into the heart of the bank
![[R language data science] (XIV): random variables and basic statistics](/img/87/3606041a588ecc615eb8013cdf9fb1.png)
[R language data science] (XIV): random variables and basic statistics

The difference between V-IF and v-show

杰理之串口接收 IO 需要设置数字功能【篇】
随机推荐
Win10 system problems
从谭浩强《C程序设计》上摘录的ASCII码表(常用字符与ASCII代码对照表)
Puzzle (016.2) finger painting Galaxy
杰理之红外滤波【篇】
融云通信“三板斧”,“砍”到了银行的心坎上
鲲鹏arm服务器编译安装PaddlePaddle
greendao使用问题
数字臧品系统开发 NFT数字臧品系统异常处理源码分享
Kotlin coordination channel
HarmonyOS.2
数据库注意事项
Research and development practice of Kwai real-time data warehouse support system
pip uninstall all packages except builtin package
P2PDB 白皮书
Jupyter notebook操作
Télétravail: Camping à la maison gadgets de bureau | rédaction communautaire
Unit contour creation method
pip uninstall all packages except builtin package
[leetcode] 10. Regular expression matching
Method of inputting dots under letters in markdown/latex