fn_b/app/Providers/NavServiceProvider.php
2026-02-12 12:36:53 +08:00

31 lines
921 B
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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);
});
}
}