31 lines
921 B
PHP
Executable File
31 lines
921 B
PHP
Executable File
<?php
|
||
|
||
namespace App\Providers;
|
||
|
||
use Illuminate\Support\ServiceProvider;
|
||
use Illuminate\Support\Facades\View;
|
||
use Illuminate\Support\Facades\Cache;
|
||
use App\Models\Categories;
|
||
|
||
class NavServiceProvider extends ServiceProvider
|
||
{
|
||
public function boot(): void
|
||
{
|
||
// 定义宁要注入数据的视图(可以是具体的 index,也可以是布局 layouts.app)
|
||
View::composer('components.nav-bar', function ($oView) {
|
||
|
||
// 1. 缓存 1 小时 (3600秒),宁写的 1 秒太凑合了,那是“皇帝的缓存”
|
||
$iCacheTime = 86400;
|
||
|
||
// 2. 优雅地从缓存或模型拿数据
|
||
$aNavs = Cache::remember('global_nav_tree', $iCacheTime, function () {
|
||
return Categories::getGlobalNavTree();
|
||
});
|
||
|
||
// 3. 把这份艺术品递给视图
|
||
$oView->with('aNavs', $aNavs);
|
||
});
|
||
|
||
}
|
||
}
|