当前位置:网站首页>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
边栏推荐
- Puzzle (016.2) finger painting Galaxy
- Successfully solved: selenium common. exceptions. SessionNotCreatedException: Message: session not created: This versi
- Unity 热力图建立方法
- R language constructs regression model diagnosis (normality is invalid), performs variable transformation, and uses powertransform function in car package to perform box Cox transform to normality on
- 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!
- [learn ZABBIX from scratch] I. Introduction and deployment of ZABBIX
- 卷积核、特征图可视化
- R language plot visualization: the visualization model creates a grid in the classification contour (contour) and meshgrid of the entire data space, in which the distance between each point is determi
- 【无标题】
- Jupyter notebook操作
猜你喜欢

Keras deep learning practice (11) -- visual neural network middle layer output

MySQL复合索引探究

Method of inputting dots under letters in markdown/latex

leetcode:1504. 统计全 1 子矩形的个数

`Thymeleaf`模板引擎全面解析

A review of text contrastive learning

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

Defeat the binary tree!

box-sizing

Digital business cloud: strengthen supplier management and promote efficient collaboration between air transport enterprises and suppliers
随机推荐
Win10 system problems
Zhiyuan community weekly 86: Gary Marcus talks about three linguistic factors that can be used for reference in large model research; Google puts forward the Wensheng graph model parti which is compar
Common singleton mode & simple factory
puzzle(016.2)指画星河
P2PDB 白皮书
【LeetCode】10、正则表达式匹配
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!
从pair到unordered_map,理论+leetcode题目实战
How to evaluate domestic reporting tools and Bi software
【比特熊故事汇】6月MVP英雄故事|技术实践碰撞境界思维
3环杀掉360安全卫士进程
PgSQL queries the largest or smallest data of a field in a group
数字臧品系统开发 NFT数字臧品系统异常处理源码分享
ssh-keygen 配置无需每次输入密码
【ansible问题处理】远程执行用户环境变量加载问题
在宇宙的眼眸下,如何正确地关心东数西算?
leetcode.12 --- 整数转罗马数字
Kunpeng arm server compilation and installation paddlepaddle
Preliminary study on AQS
4 reasons for "safe left shift"