当前位置:网站首页>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
边栏推荐
- leetcode:1504. Count the number of all 1 sub rectangles
- P2pdb white paper
- 数字臧品系统开发 NFT数字臧品系统异常处理源码分享
- Overview of SAP marketing cloud functions (IV)
- 10_ Those high-profile personal signatures
- 【Pytorch】量化
- [ansible problem processing] remote execution user environment variable loading problem
- 不要小看了积分商城,它的作用可以很大
- 六石管理学:垃圾场效应:工作不管理,就会变成垃圾场
- 15 differences between MES in process and discrete manufacturing enterprises (Part 2)
猜你喜欢

GO语言-goroutine协程的使用
![Maximum path sum in binary tree [handle any subtree, then handle the whole tree]](/img/d0/91ab1cc1851d7137a1cab3cf458302.png)
Maximum path sum in binary tree [handle any subtree, then handle the whole tree]

作为一名开发者,对你影响最深的书籍是哪一本?

tongweb使用之端口冲突处理办法

One click to generate University, major and even admission probability. Is it so magical for AI to fill in volunteer cards?
![Generate binary tree according to preorder & inorder traversal [partition / generation / splicing of left subtree | root | right subtree]](/img/f7/8d026c0e4435fc8fd7a63616b4554d.png)
Generate binary tree according to preorder & inorder traversal [partition / generation / splicing of left subtree | root | right subtree]

食品饮料行业渠道商管理系统解决方案:实现渠道数字化营销布局

v-for 中 key的作用和原理

遠程辦公之:在家露營辦公小工具| 社區征文

In the eyes of the universe, how to correctly care about counting East and West?
随机推荐
Jupyter notebook操作
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
A review of text contrastive learning
卷积核、特征图可视化
pgsql查询分组中某个字段最大或者最小的一条数据
Database considerations
R语言plotly可视化:使用plotly可视化数据划分后的训练集和测试集、使用不同的形状标签表征、训练集、测试集、以及数据集的分类标签(Display training and test split
成功解决:selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This versi
Daily knowledge popularization
怎样评价国产报表工具和BI软件
laravel下视图间共享数据
Method of establishing unity thermodynamic diagram
Second, the examinee must see | consolidate the preferred question bank to help the examinee make the final dash
【Pytorch】量化
leetcode.12 --- 整数转罗马数字
Telecommuting: camping at home office gadgets | community essay solicitation
专精特新“小巨人”再启动,“企业上云”数字赋能
[ansible problem processing] remote execution user environment variable loading problem
GO语言并发模型-MPG模型
[pytoch] quantification