laravel:所有view共享数据(10.27.0)

一,php代码:

1,app\providers\AppServiceProvider.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
 
namespace App\Providers;
 
use Illuminate\Support\ServiceProvider;
 
use Illuminate\Support\Facades\View;
 
class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }
 
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        //
        $site = [
            'name' => '游戏装具专业工厂',
            'host' => 'www.laoliugame.com',
        ];
 
        config(['global_site' => $site]);
        View::share('global_site', $site);
    }
}

2,controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//home页面
public function home(Request $request){
 
    //修改通用数据
    $rowSite = config('global_site');
    $rowSite['name'] = "开始学习laravel";
    View::share('global_site', $rowSite);
 
    //返回给页面的数据
    $goodsName = '元青花宝瓶仿制品';
 
    $links = [
        ["name"=>"新浪","url"=>"https://www.sina.com.cn"],
        ["name"=>"百度","url"=>"https://baidu.com"],
    ];
 
    return view('pages.home', [
        'goodsName' => $goodsName,
        'title' => 'laravel教学网',
        'links' => $links,
    ]);
}

3,header.blade.php

1
2
3
4
5
<div id="header" style="width:100%;height:100px;background: #ffff00;">
    这里是头部<br/>
    当前站点名字:{{$global_site['name']}}<br/>
    当前站点host:{{$global_site['host']}}
</div>

说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/10/30/laravel-suo-you-view-gong-xiang-shu-ju-10-27/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,测试效果:

三,查看laravel框架的版本:

liuhongdi@lhdpc:/data/laravel/dignews$ php artisan --version
Laravel Framework 10.27.0
posted @ 2023-10-31 08:21  刘宏缔的架构森林  阅读(8)  评论(0编辑  收藏  举报