当前位置:网站首页>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
边栏推荐
- Method of inputting dots under letters in markdown/latex
- 六月集训(第23天) —— 字典树
- Virtual machines on the same distributed port group but different hosts cannot communicate with each other
- PgSQL queries the largest or smallest data of a field in a group
- pgsql查询分组中某个字段最大或者最小的一条数据
- Some basic database operations (providing the original database information)
- 【从零开始学zabbix】一丶Zabbix的介绍与部署Zabbix
- Antd checkbox, limit the selected quantity
- 高薪程序员&面试题精讲系列115之Redis缓存如何实现?怎么发现热key?缓存时可能存在哪些问题?
- Qunhui synchronizes with alicloud OSS
猜你喜欢

box-sizing

IDEA连接mysql自定义生成实体类代码

OpenHarmony 1

P2pdb white paper

作为一名开发者,对你影响最深的书籍是哪一本?
![二叉树中最大路径和[处理好任意一颗子树,就处理好了整个树]](/img/d0/91ab1cc1851d7137a1cab3cf458302.png)
二叉树中最大路径和[处理好任意一颗子树,就处理好了整个树]

从谭浩强《C程序设计》上摘录的ASCII码表(常用字符与ASCII代码对照表)

postgresql之词法分析简介
![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]

【比特熊故事汇】6月MVP英雄故事|技术实践碰撞境界思维
随机推荐
港股上市公司公告 API 数据接口
Mit-6.824-lab4a-2022 (ten thousand words explanation - code construction)
leetcode:1504. Count the number of all 1 sub rectangles
MySQL复合索引探究
【深度学习】NCHW、NHWC和CHWN格式数据的存储形式
Digital business cloud: strengthen supplier management and promote efficient collaboration between air transport enterprises and suppliers
In the eyes of the universe, how to correctly care about counting East and West?
GO语言-init()函数-包初始化
box-sizing
Overview of SAP marketing cloud functions (III)
Grendao usage problems
数据库一些基本操作(提供了原数据库信息)
box-sizing
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!
【比特熊故事汇】6月MVP英雄故事|技术实践碰撞境界思维
智慧园区SaaS管理系统解决方案:赋能园区实现信息化、数字化管理
【无标题】
The difference between V-IF and v-show
MySQL日志管理、备份与恢复