50 lines
1.3 KiB
PHP
50 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');
|
|
$horizontalMenuPath = base_path('resources/menu/horizontalMenu.json');
|
|
|
|
// 確保文件存在
|
|
if (!file_exists($verticalMenuPath) || !file_exists($horizontalMenuPath)) {
|
|
throw new \Exception('Menu JSON files are missing.');
|
|
}
|
|
|
|
// 讀取並解析 JSON 文件
|
|
$verticalMenuJson = file_get_contents($verticalMenuPath);
|
|
$horizontalMenuJson = file_get_contents($horizontalMenuPath);
|
|
|
|
$verticalMenuData = json_decode($verticalMenuJson);
|
|
$horizontalMenuData = json_decode($horizontalMenuJson);
|
|
|
|
// 確保解析成功
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
throw new \Exception('Error parsing menu JSON: ' . json_last_error_msg());
|
|
}
|
|
|
|
// 分享數據到所有視圖
|
|
view()->share('menuData', [
|
|
$verticalMenuData,
|
|
$horizontalMenuData,
|
|
]);
|
|
}
|
|
}
|