cafeg/app/Providers/MenuProvider.php
2025-01-20 16:42:05 +08:00

49 lines
1.3 KiB
PHP

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class MenuProvider extends ServiceProvider
{
/**
* Register services.
*/
public function register(): void
{
// 此處可注冊其他服務
}
/**
* Bootstrap services.
*/
public function boot(): void
{
// 定義垂直與水平菜單的 JSON 文件路徑
$verticalMenuPath = base_path('resources/menu/verticalMenu.json');
$memberMenuPath = base_path('resources/menu/memberMenu.json');
// 確保文件存在
if (! file_exists($verticalMenuPath) || ! file_exists($memberMenuPath)) {
throw new \Exception('Menu JSON files are missing.');
}
// 讀取並解析 JSON 文件
$verticalMenuJson = file_get_contents($verticalMenuPath);
$memberMenuJson = file_get_contents($memberMenuPath);
$verticalMenuData = json_decode($verticalMenuJson);
$memberMenuData = json_decode($memberMenuJson);
// 確保解析成功
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception('Error parsing menu JSON: ' . json_last_error_msg());
}
// 分享數據到所有視圖
view()->share('menuData', [
$verticalMenuData,
$memberMenuData,
]);
}
}