当前位置:网站首页>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

 Insert picture description here
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

原网站

版权声明
本文为[ignativs amor]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241218061533.html