当前位置:网站首页>Data sharing between laravel lower views
Data sharing between laravel lower views
2022-06-24 14:23:00 【ignativs amor】
background
Recently, I was upgrading a very old project , Finally used laravel8 Processing items , The project is complicated , The front end has three login and registration functions ; So after logging in , You need to transfer the login account and the permissions of the account to the page , Data sharing is designed here ;
The process
Method 1 : Using the controller
Define three basic controllers , stay __construct() To obtain the required login user information , Then the module that needs this information inherits the corresponding basic controller ( notes :laravel The execution order is to construct the method first , Then middleware )
Method 2 : Try the facade share() Method realization

And then in boot() Method to test :
View::share('name1', 'first name');
// You can also use
view()->share('name2','second name');
You can add... Anywhere on the front page $name1 and $name2
In this way, all front-end pages can display the shared data , But the downside is , stay AppServiceProvider Cannot get cache in this function , You can only query some directional data from the database or customize some variable values , It has great limitations in use
Method 3 : View facade composer() Method realization
A view builder is a callback or class method that is called when a view is rendered . If you want to automatically bind data to the view each time you render the view , Then the view generator can help you organize these logic . If multiple routes or controllers in the application return to the same view , And always need specific data , Then view generators can be particularly useful .
Usually , The view generator is somewhere in the application Service providers Registered in the .
In this case , We will create a new \Providers\ViewShareServiceProvider To accommodate this logic .
We will use View facade Of composer Method to register the view builder .Laravel There is no default directory for the view builder , So you can organize them at will . for instance , You can create a app/Http/View/Composers Directory to store all view generators in the application :
- step 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
{
/** * Sign up for application services * * @return void */
public function register()
{
//
}
/** * Guiding application services * * @return void */
public function boot()
{
// Use class based generators
View::composer('enterprise.*', HrComposer::class);//hr
View::composer(['pc.*','hunter.*'], UserComposer::class);//user
}
}
- step 2
If you create a new service provider to store the code for your registered view generator , Then you need to add this service provider to the configuration file config/app.php Of providers Array . - step 3
Registered view builder , Every time you render a view , It will be carried out App\Http\View\Composers\HrComposer Class compose Method . Now let's define the class of view generator
<?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;
/** * Create a View generator * * @return void */
public function __construct()
{
// Dependencies are automatically resolved by the service container ...
}
public function compose(View $view){
// Get the login enterprise information
$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
]);
}
}
result
such , When the view is about to render , Execution attempt generator , Automatically bind data to views , Try inside , You can get the data you need
边栏推荐
- 六月集训(第24天) —— 线段树
- 不要小看了积分商城,它的作用可以很大
- 数字臧品系统开发 NFT数字臧品系统异常处理源码分享
- postgresql 之 ilist
- Maximum path sum in binary tree [handle any subtree, then handle the whole tree]
- 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!
- GO语言-goroutine协程的使用
- One click to generate University, major and even admission probability. Is it so magical for AI to fill in volunteer cards?
- 文本对比学习综述
- Overview of SAP marketing cloud functions (IV)
猜你喜欢

Mit-6.824-lab4a-2022 (ten thousand words explanation - code construction)

c语言---18 函数(自定义函数)

win10系统问题

In the eyes of the universe, how to correctly care about counting East and West?

Linux 安装 CenOS7 MySQL - 8.0.26

The function and principle of key in V-for

The difference between V-IF and v-show

postgresql之词法分析简介

Idea connection MySQL custom generated entity class code

Overview of SAP marketing cloud functions (IV)
随机推荐
Getting to know cloud native security for the first time: the best guarantee in the cloud Era
Keras深度学习实战(11)——可视化神经网络中间层输出
markdown/LaTeX中在字母下方输入圆点的方法
PgSQL queries the largest or smallest data of a field in a group
Halcon draw area into picture
The "little giant" specialized in special new products is restarted, and the "enterprise cloud" digital empowerment
厨卫电器行业B2B交易协同管理平台开发,优化企业库存结构
MySQL title
c语言---18 函数(自定义函数)
R语言plotly可视化:可视化模型在整个数据空间的分类轮廓线(等高线)、meshgrid创建一个网格,其中每个点之间的距离由mesh_size变量表示、使用不同的形状标签表征、训练、测试及分类标签
laravel下视图间共享数据
leetcode 139. Word Break 单词拆分(中等)
21set classic case
ESP32系列--ESP32各个系列对比
Puzzle (016.2) finger painting Galaxy
Keras deep learning practice (11) -- visual neural network middle layer output
MES在流程和离散制造企业的15个差别(下)
专精特新“小巨人”再启动,“企业上云”数字赋能
SSH keygen configuration does not require entering a password every time
如何避免下重复订单