Laravel Filamentとは:管理画面開発の革新的ツール
Laravel Filamentは、PHPのフレームワークLaravelで高品質な管理画面(管理パネル)を素早く開発するためのフルスタックのアドミン構築パッケージです。従来の管理画面開発で直面していた煩雑な実装作業や保守性の課題を解決し、開発者が本質的なビジネスロジックに集中できる環境を提供します。
従来の管理画面開発における課題と解決策
管理画面開発では、以下のような課題が常につきまとっていました:
- 開発工数の膨大さ
- データの一覧表示、検索、ソート機能の実装
- フォーム作成とバリデーション処理
- 権限管理システムの構築
- 保守性とスケーラビリティ
- 機能追加時のコード複雑化
- UIの一貫性維持の難しさ
- テストコードの作成と維持
Filamentは、これらの課題に対して以下のソリューションを提供します:
- 自動生成される管理画面:モデルの定義だけで基本的なCRUD操作画面を自動生成
- 豊富なUIコンポーネント:一貫性のあるデザインの管理画面を素早く構築可能
- 柔軟なカスタマイズ性:ビジネスニーズに応じた拡張が容易
- 堅牢な権限管理:Laravel標準の認証機能との seamless な統合
Filamentが提供する主要な機能と特徴
- パネルビルダー
- リソース(CRUD)管理
- ダッシュボードウィジェット
- グローバル検索
- カスタムページ作成
- フォームビルダー
- 40種類以上のフォームコンポーネント
- バリデーション機能の統合
- ファイルアップロード処理
- リレーション処理の簡素化
- テーブルビルダー
- 高度なフィルタリング
- カスタムアクション
- バルク操作
- レスポンシブ対応
- 認証・認可
- RBACによる権限管理
- ポリシーベースのアクセス制御
- カスタム認証の統合
他の管理画面パッケージとの比較
| 機能 | Filament | Nova | Voyager |
|---|---|---|---|
| 価格 | 無料 | 有料 | 無料 |
| 学習曲線 | 緩やか | 中程度 | 急 |
| カスタマイズ性 | 高 | 中 | 低 |
| コミュニティ | 活発 | 活発 | 低調 |
| ドキュメント | 充実 | 充実 | 普通 |
| UIコンポーネント数 | 多い | 中程度 | 少ない |
Filamentの特筆すべき強みは、無料でありながら高度なカスタマイズが可能な点です。また、活発なコミュニティによる継続的な改善と、充実したドキュメントにより、開発者は必要な情報やサポートを容易に得ることができます。
次のセクションでは、実際にFilamentを導入し、基本的な設定を行う手順について詳しく解説します。
Filamentの環境構築と初期設定
Filamentを効果的に活用するためには、適切な環境構築と初期設定が不可欠です。このセクションでは、開発環境の準備から初期設定まで、順を追って解説していきます。
必要な要件と開発環境の準備
Filamentを使用するには、以下の要件を満たす必要があります:
必須要件:
- PHP 8.1以上
- Laravel 10.0以上
- Composer 2.0以上
- Node.js 16以上
- データベース(MySQL、PostgreSQL、SQLiteのいずれか)
推奨要件:
- メモリ:2GB以上のRAM
- ストレージ:空き容量1GB以上
- モダンなブラウザ(Chrome、Firefox、Safari、Edge)
開発環境のセットアップ手順:
# Laravelプロジェクトの作成 composer create-project laravel/laravel filament-admin # プロジェクトディレクトリへ移動 cd filament-admin # データベースの設定 # .envファイルを編集してデータベース接続情報を設定 DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=filament_admin DB_USERNAME=root DB_PASSWORD=
Composer を使用したインストール手順
Filamentのインストールは、Composerを使用して簡単に行えます:
# Filament本体のインストール composer require filament/filament:"^3.0" # パネル作成に必要なアセットのインストール php artisan filament:install --panels # アセットのビルド npm install npm run build
インストール後の確認ポイント:
config/filament.phpの存在確認resources/cssとresources/jsにFilamentのアセットが追加されているかdatabase/migrationsにユーザーテーブルのマイグレーションファイルがあるか
初期設定とカスタマイズのポイント
1. 管理者ユーザーの作成
# 管理者ユーザーの作成コマンド
php artisan make:filament-user
# または以下のコマンドでシーダーを作成
php artisan make:seeder AdminUserSeeder
# AdminUserSeeder.phpの内容
namespace Database\Seeders;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
class AdminUserSeeder extends Seeder
{
public function run(): void
{
User::create([
'name' => 'Admin User',
'email' => 'admin@example.com',
'password' => Hash::make('password'),
]);
}
}
2. パネルのカスタマイズ
// app/Providers/Filament/AdminPanelProvider.php
public function panel(Panel $panel): Panel
{
return $panel
->default()
->id('admin')
->path('admin') // URLパスの設定
->login() // ログイン機能の有効化
->colors([
'primary' => Color::Amber,
])
->discoverResources(in: app_path('Filament/Resources'))
->discoverPages(in: app_path('Filament/Pages'))
->pages([
Pages\Dashboard::class,
])
->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
]);
}
3. 多言語対応の設定
// config/app.php
'locale' => 'ja',
'fallback_locale' => 'en',
// 言語ファイルの作成
// lang/ja/filament.php
return [
'pages' => [
'dashboard' => [
'title' => 'ダッシュボード',
],
],
'resources' => [
'title' => 'リソース',
],
];
4. データベースの設定確認
# マイグレーションの実行 php artisan migrate # テーブルの確認 php artisan db:show # シーダーの実行(管理者ユーザー作成) php artisan db:seed --class=AdminUserSeeder
設定のベストプラクティス
- セキュリティ設定
- 強力なパスワードポリシーの設定
- セッションタイムアウトの適切な設定
- HTTPS強制の設定
- パフォーマンス最適化
- キャッシュの適切な設定
- アセットの最適化
- データベースインデックスの設定
- 開発環境固有の設定
- デバッグモードの適切な設定
- ログレベルの調整
- 開発用の環境変数の設定
これらの設定が完了したら、http://localhost/admin にアクセスして管理画面が正しく表示されることを確認してください。次のセクションでは、基本的なCRUD操作の実装方法について詳しく解説します。
基本的なCRUDオペレーションの実装方法
Filamentを使用したCRUD(Create, Read, Update, Delete)操作の実装について、実践的な例を交えながら解説します。ここでは、一般的な「商品管理システム」を例に、基本的な実装手順から実践的なテクニックまでを紹介します。
リソースクラスの作成と設定
1. モデルとマイグレーションの準備
# 商品モデルとマイグレーションの作成 php artisan make:model Product -m # Filamentリソースの作成 php artisan make:filament-resource Product
// database/migrations/xxxx_xx_xx_create_products_table.php
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description')->nullable();
$table->decimal('price', 10, 2);
$table->integer('stock')->default(0);
$table->boolean('is_active')->default(true);
$table->string('category')->nullable();
$table->timestamps();
});
}
// app/Models/Product.php
class Product extends Model
{
protected $fillable = [
'name',
'description',
'price',
'stock',
'is_active',
'category'
];
protected $casts = [
'price' => 'decimal:2',
'is_active' => 'boolean',
];
}
2. リソースクラスの設定
// app/Filament/Resources/ProductResource.php
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Forms;
class ProductResource extends Resource
{
protected static ?string $model = Product::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
protected static ?string $navigationGroup = '商品管理';
protected static ?int $navigationSort = 1;
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Card::make()
->schema([
Forms\Components\TextInput::make('name')
->label('商品名')
->required()
->maxLength(255),
Forms\Components\RichEditor::make('description')
->label('商品説明')
->columnSpan('full'),
Forms\Components\TextInput::make('price')
->label('価格')
->required()
->numeric()
->prefix('¥'),
Forms\Components\TextInput::make('stock')
->label('在庫数')
->numeric()
->default(0),
Forms\Components\Select::make('category')
->label('カテゴリー')
->options([
'electronics' => '電化製品',
'clothing' => '衣類',
'food' => '食品',
]),
Forms\Components\Toggle::make('is_active')
->label('販売状態')
->default(true),
])
->columns(2)
]);
}
}
フォームの作成とバリデーション
1. カスタムバリデーションルールの実装
// app/Filament/Resources/ProductResource.php
use Illuminate\Validation\Rules\Unique;
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->label('商品名')
->required()
->maxLength(255)
->unique(ignoreRecord: true)
->rules([
fn () => new Unique('products', 'name')
]),
Forms\Components\TextInput::make('price')
->label('価格')
->required()
->numeric()
->rules([
'min:0',
'max:999999999'
])
->mask(fn (Forms\Components\TextInput\Mask $mask) => $mask
->numeric()
->thousandsSeparator(',')
),
]);
}
// カスタムバリデーションメッセージの設定
protected function getMessages(): array
{
return [
'name.required' => '商品名は必須です',
'name.unique' => 'この商品名は既に使用されています',
'price.min' => '価格は0以上を指定してください',
];
}
リレーション処理の実践テクニック
1. リレーションの定義
// app/Models/Product.php
public function category()
{
return $this->belongsTo(Category::class);
}
public function images()
{
return $this->hasMany(ProductImage::class);
}
// app/Filament/Resources/ProductResource.php
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
public static function form(Form $form): Form
{
return $form
->schema([
// ... 他のフィールド
Forms\Components\Select::make('category_id')
->relationship('category', 'name')
->searchable()
->preload(),
SpatieMediaLibraryFileUpload::make('images')
->label('商品画像')
->multiple()
->maxFiles(5)
->image()
->imageResizeMode('cover')
->imageCropAspectRatio('16:9')
->imageResizeTargetWidth('1920')
->imageResizeTargetHeight('1080'),
]);
}
2. 一括操作の実装
// app/Filament/Resources/ProductResource.php
use Filament\Tables\Actions\BulkAction;
use Illuminate\Database\Eloquent\Collection;
public static function table(Table $table): Table
{
return $table
->columns([
// ... カラムの定義
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
BulkAction::make('updateStock')
->label('在庫数更新')
->icon('heroicon-o-arrow-path')
->action(function (Collection $records, array $data): void {
$records->each(function ($record) use ($data): void {
$record->update([
'stock' => $data['stock'],
]);
});
})
->form([
Forms\Components\TextInput::make('stock')
->label('在庫数')
->required()
->numeric()
->default(0),
]),
]);
}
実装のベストプラクティス
- パフォーマンス最適化
- Eagerローディングの活用
- インデックスの適切な設定
- キャッシュの活用
- セキュリティ対策
- 入力値の適切なサニタイズ
- アクセス制御の実装
- CSRFトークンの確認
- コード品質の維持
- PSR-12コーディング規約の遵守
- PHPDocによるドキュメント化
- 適切な名前空間の使用
- テスト可能性の確保
- ユニットテストの作成
- フィーチャーテストの実装
- CIでの自動テスト実行
次のセクションでは、より高度な機能とカスタマイズ方法について解説します。
高機能とカスタマイズ
Filamentの基本機能を拡張し、より高度な管理画面を構築するためのカスタマイズ方法について解説します。ダッシュボードの充実化から認証・認可の実装、UIのカスタマイズまで、実践的なテクニックを紹介します。
ダッシュボードウィジェットの活用法
1. カスタムウィジェットの作成
// app/Filament/Widgets/StatsOverview.php
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
use App\Models\Product;
use App\Models\Order;
class StatsOverview extends BaseWidget
{
protected function getStats(): array
{
return [
Stat::make('総商品数', Product::count())
->description('先月比 +20%')
->descriptionIcon('heroicon-m-arrow-trending-up')
->chart([7, 4, 6, 8, 5, 9, 10])
->color('success'),
Stat::make('本日の売上', '¥' . number_format(Order::whereDate('created_at', today())->sum('total')))
->description('昨日比 -2%')
->descriptionIcon('heroicon-m-arrow-trending-down')
->color('danger'),
Stat::make('在庫切れ商品', Product::where('stock', 0)->count())
->description('要対応')
->color('warning'),
];
}
}
// app/Filament/Widgets/LatestOrders.php
use Filament\Widgets\TableWidget as BaseWidget;
use Illuminate\Database\Eloquent\Builder;
class LatestOrders extends BaseWidget
{
protected int | string | array $columnSpan = 'full';
protected function getTableQuery(): Builder
{
return Order::query()
->latest()
->limit(5);
}
protected function getTableColumns(): array
{
return [
Tables\Columns\TextColumn::make('id')
->label('注文番号'),
Tables\Columns\TextColumn::make('customer.name')
->label('顧客名'),
Tables\Columns\TextColumn::make('total')
->label('合計金額')
->money('jpy'),
Tables\Columns\BadgeColumn::make('status')
->label('状態')
->colors([
'warning' => 'pending',
'success' => 'completed',
'danger' => 'cancelled',
]),
];
}
}
2. リアルタイムダッシュボードの実装
// app/Filament/Widgets/RealtimeStats.php
use Filament\Widgets\Widget;
use Livewire\Component;
class RealtimeStats extends Widget implements HasFrontendInteractions
{
public $listeners = ['echo:orders,OrderCreated' => '$refresh'];
protected static string $view = 'filament.widgets.realtime-stats';
protected int | string | array $columnSpan = 'full';
public function getFrontendData(): array
{
return [
'salesData' => $this->getSalesData(),
'orderCount' => $this->getOrderCount(),
];
}
protected function getSalesData(): array
{
return Order::select(DB::raw('DATE(created_at) as date'), DB::raw('SUM(total) as total'))
->groupBy('date')
->get()
->toArray();
}
}
認証・認可の実装方法
1. カスタム認証の設定
// app/Providers/Filament/AdminPanelProvider.php
use App\Filament\Auth\Login;
public function panel(Panel $panel): Panel
{
return $panel
->login(Login::class)
->authGuard('admin')
->authPasswordBroker('admins')
->registration(false)
->passwordReset(false)
->emailVerification(true);
}
// app/Filament/Auth/Login.php
use Filament\Pages\Auth\Login as BaseLogin;
class Login extends BaseLogin
{
protected function getFormSchema(): array
{
return [
TextInput::make('email')
->label('メールアドレス')
->email()
->required(),
TextInput::make('password')
->label('パスワード')
->password()
->required(),
Toggle::make('remember')
->label('ログイン状態を保持'),
];
}
protected function authenticate(): void
{
// カスタム認証ロジックの実装
}
}
2. 権限管理の実装
// app/Policies/ProductPolicy.php
class ProductPolicy
{
public function viewAny(User $user): bool
{
return $user->can('view_products');
}
public function create(User $user): bool
{
return $user->can('create_products');
}
// ... 他の権限メソッド
}
// app/Filament/Resources/ProductResource.php
public static function getPermissionPrefixes(): array
{
return [
'view',
'create',
'update',
'delete',
'restore',
];
}
protected static function getNavigationBadge(): ?string
{
return static::getModel()::count();
}
テーマのカスタマイズとUIの改善
1. カスタムテーマの作成
// resources/css/filament/admin/theme.css
@import '/vendor/filament/filament/resources/css/theme.css';
:root {
--primary-50: 240 249 255;
--primary-100: 224 242 254;
--primary-200: 186 230 253;
--primary-300: 125 211 252;
--primary-400: 56 189 248;
--primary-500: 14 165 233;
--primary-600: 2 132 199;
--primary-700: 3 105 161;
--primary-800: 7 89 133;
--primary-900: 12 74 110;
--primary-950: 8 47 73;
}
// tailwind.config.js
const colors = require('tailwindcss/colors')
module.exports = {
content: [
'./resources/**/*.blade.php',
'./vendor/filament/**/*.blade.php',
],
theme: {
extend: {
colors: {
danger: colors.rose,
primary: colors.blue,
success: colors.green,
warning: colors.yellow,
},
},
},
plugins: [
require('@tailwindcss/forms'),
require('@tailwindcss/typography'),
],
}
2. カスタムレイアウトの実装
// app/Filament/Pages/CustomDashboard.php
use Filament\Pages\Dashboard as BaseDashboard;
class CustomDashboard extends BaseDashboard
{
protected static string $view = 'filament.pages.dashboard';
protected function getHeaderWidgets(): array
{
return [
StatsOverview::class,
RealtimeStats::class,
];
}
protected function getFooterWidgets(): array
{
return [
LatestOrders::class,
];
}
}
// resources/views/filament/pages/dashboard.blade.php
<x-filament::page>
<div class="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3">
@foreach ($this->getHeaderWidgets() as $widget)
{{ $widget }}
@endforeach
</div>
<div class="mt-4">
@foreach ($this->getFooterWidgets() as $widget)
{{ $widget }}
@endforeach
</div>
</x-filament::page>
カスタマイズのベストプラクティス:
- パフォーマンス最適化
- アセットの最適化(ミニファイ、圧縮)
- キャッシュ戦略の実装
- 遅延読み込みの活用
- セキュリティ強化
- 適切なCSRF保護
- XSS対策の実装
- レート制限の設定
- 保守性の向上
- モジュール化された設計
- 一貫性のあるコーディング規約
- 適切なドキュメント化
次のセクションでは、実践的な開発テクニックについて詳しく解説します。
実践的な開発テクニック
Filamentを使用した実務開発において、パフォーマンス、テスト、実装の各側面における実践的なテクニックを解説します。この知識は、大規模なプロジェクトや長期的な運用において特に重要となります。
パフォーマンス最適化のベストプラクティス
1. データベースクエリの最適化
// app/Filament/Resources/OrderResource.php
use Filament\Resources\Resource;
use Illuminate\Database\Eloquent\Builder;
class OrderResource extends Resource
{
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->with(['customer', 'products']) // Eager Loading
->withCount('products')
->withSum('products', 'price')
->latest();
}
public static function table(Table $table): Table
{
return $table
->columns([
// 必要な列のみを表示
Tables\Columns\TextColumn::make('id')
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('customer.name')
->searchable()
->sortable(),
])
->defaultSort('created_at', 'desc')
// ページネーションの最適化
->paginated([10, 25, 50, 100])
->persistFiltersInSession()
->persistSortInSession();
}
}
// キャッシュの活用
class ProductResource extends Resource
{
protected static function getNavigationBadge(): ?string
{
return Cache::remember('products_count', 3600, function () {
return static::getModel()::count();
});
}
}
2. フロントエンドの最適化
// config/filament.php
return [
'assets' => [
'should_minify_scripts' => true,
'should_minify_styles' => true,
],
'cache' => [
'ttl' => 3600,
],
];
// Blade コンポーネントの最適化
class CustomProductList extends Component
{
use WithPagination;
public function render()
{
return view('livewire.custom-product-list', [
'products' => Product::query()
->when($this->search, fn ($query) =>
$query->where('name', 'like', "%{$this->search}%")
)
->paginate(10),
]);
}
}
テストの書き込みと自動化の方法
1. 基本的なテストの実装
// tests/Feature/ProductResourceTest.php
namespace Tests\Feature;
use App\Filament\Resources\ProductResource;
use App\Models\Product;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ProductResourceTest extends TestCase
{
use RefreshDatabase;
public function test_can_list_products()
{
$this->actingAs(User::factory()->create());
$products = Product::factory()->count(10)->create();
$response = $this->get(ProductResource::getUrl('index'));
$response->assertSuccessful();
$response->assertSee($products->first()->name);
}
public function test_can_create_product()
{
$this->actingAs(User::factory()->create());
$newProduct = Product::factory()->make();
$response = $this->post(ProductResource::getUrl('create'), [
'name' => $newProduct->name,
'price' => $newProduct->price,
'description' => $newProduct->description,
]);
$response->assertRedirect();
$this->assertDatabaseHas('products', [
'name' => $newProduct->name,
]);
}
}
2. 自動テストの設定
# .github/workflows/test.yml
name: Test Suite
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Execute tests via PHPUnit
run: vendor/bin/phpunit
実務での活用事例と実践例
1. 大規模データ処理の実装
// app/Filament/Resources/ProductResource/Pages/ImportProducts.php
use Filament\Resources\Pages\Page;
use Illuminate\Support\Facades\Bus;
use Maatwebsite\Excel\Facades\Excel;
class ImportProducts extends Page
{
protected static string $resource = ProductResource::class;
protected static string $view = 'filament.resources.product-resource.pages.import-products';
public function import()
{
$this->validate([
'file' => ['required', 'file', 'mimes:xlsx,csv'],
]);
$import = new ProductsImport;
// バッチ処理による大量データのインポート
Bus::batch([
new ProcessProductImport($this->file)
])->dispatch();
Notification::make()
->title('インポートを開始しました')
->success()
->send();
}
}
// バッチ処理用のジョブクラス
class ProcessProductImport implements ShouldQueue
{
use Batchable, Queueable;
public function handle()
{
Excel::queueImport(new ProductsImport, $this->file);
}
}
2. カスタムアクションの実装
// app/Filament/Resources/OrderResource/Actions/GenerateInvoiceAction.php
use Filament\Tables\Actions\Action;
use Filament\Notifications\Notification;
class GenerateInvoiceAction extends Action
{
protected function setUp(): void
{
$this->label('請求書生成')
->icon('heroicon-o-document-text')
->action(function ($record) {
// PDF生成処理
$pdf = PDF::loadView('invoices.template', [
'order' => $record,
]);
// S3への保存
Storage::disk('s3')->put(
"invoices/{$record->id}.pdf",
$pdf->output()
);
Notification::make()
->title('請求書を生成しました')
->success()
->send();
})
->requiresConfirmation();
}
}
実装のベストプラクティス
- コード品質の維持
- PHPStanによる静的解析
- PHP_CodeSnifferによるコーディング規約チェック
- GitHubActionsによる自動チェック
- 開発効率の向上
- 共通コンポーネントの作成
- カスタムフォームフィールドの実装
- 再利用可能なトレイトの活用
- チーム開発での注意点
- 命名規則の統一
- ドキュメントの整備
- コードレビューの基準設定
次のセクションでは、トラブルシューティングとメンテナンスについて解説します。
トラブルシューティングとメンテナンス
Filamentを用いた管理画面の運用において発生する可能性のある問題とその解決方法、そして長期的な保守について解説します。
よくあるエラーと解決方法
1. インストール・アップデート時の問題
# 1. コンポーネントの競合エラー Composer エラー: "Package filament/filament has requirements incompatible with your PHP version" # 解決方法 composer update --ignore-platform-reqs composer install --ignore-platform-reqs # 2. アセットのビルドエラー npm ERR! Failed at the build script # 解決方法 rm -rf node_modules rm package-lock.json npm cache clean --force npm install npm run build
2. 実行時エラーの対処
// 1. メモリ不足エラーの対処
// php.ini または .htaccess で設定
ini_set('memory_limit', '512M');
// 2. タイムアウトエラーの対処
// app/Providers/AppServiceProvider.php
public function boot()
{
ini_set('max_execution_time', 300); // 5分
}
// 3. ファイルアップロードエラーの対処
// config/filament.php
return [
'max_upload_size' => 50 * 1024, // 50MB
];
// 4. データベースクエリタイムアウトの対処
// config/database.php
'mysql' => [
'options' => [
PDO::ATTR_TIMEOUT => 60,
],
],
エラーログの監視と分析
// app/Exceptions/Handler.php
use Illuminate\Support\Facades\Log;
class Handler extends ExceptionHandler
{
public function register(): void
{
$this->reportable(function (Throwable $e) {
if ($this->shouldReport($e)) {
Log::error('Filament Error:', [
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString(),
'user' => auth()->user()?->id,
'url' => request()->url(),
]);
}
});
}
}
// カスタムログチャンネルの設定
// config/logging.php
'channels' => [
'filament' => [
'driver' => 'daily',
'path' => storage_path('logs/filament.log'),
'level' => env('LOG_LEVEL', 'debug'),
'days' => 14,
],
],
バージョンアップ時の注意点
1. アップグレード手順のベストプラクティス
# 1. 現在の環境のバックアップ php artisan backup:run # 2. 依存関係の更新 composer update filament/filament --with-dependencies # 3. キャッシュのクリア php artisan optimize:clear php artisan view:clear php artisan cache:clear # 4. データベースマイグレーション php artisan migrate # 5. アセットの再ビルド npm install npm run build
2. 破壊的変更への対応
// 1. 非推奨機能の確認と更新
// 古い実装
use Filament\Resources\Table;
// 新しい実装
use Filament\Tables\Table;
// 2. カスタムコンポーネントの互換性確保
class CustomField extends Field
{
protected function setUp(): void
{
// 新しいAPIへの対応
parent::setUp();
// 後方互換性の維持
if (version_compare(Filament::VERSION, '3.0.0', '<')) {
$this->legacy();
}
}
}
長期運用のためのメンテナンス方法
1. 定期的なメンテナンスタスク
// app/Console/Kernel.php
protected function schedule(Schedule $schedule): void
{
// データベースの最適化
$schedule->command('db:optimize')->weekly();
// キャッシュの更新
$schedule->command('filament:cache')->daily();
// バックアップの作成
$schedule->command('backup:clean')->daily();
$schedule->command('backup:run')->daily();
// ログのクリーンアップ
$schedule->command('log:clean')->monthly();
}
// app/Console/Commands/DbOptimize.php
class DbOptimize extends Command
{
public function handle()
{
// テーブルの最適化
DB::statement('OPTIMIZE TABLE products, orders, users');
// 古いデータの整理
Order::where('created_at', '<', now()->subYears(2))
->delete();
}
}
2. パフォーマンスモニタリング
// config/telescope.php
return [
'enabled' => env('TELESCOPE_ENABLED', true),
'watchers' => [
Watchers\QueryWatcher::class => [
'enabled' => env('TELESCOPE_QUERY_WATCHER', true),
'slow' => 100,
],
Watchers\RequestWatcher::class => [
'enabled' => env('TELESCOPE_REQUEST_WATCHER', true),
'size_limit' => env('TELESCOPE_RESPONSE_SIZE_LIMIT', 64),
],
],
];
// パフォーマンスメトリクスの収集
class PerformanceMiddleware
{
public function handle($request, Closure $next)
{
$start = microtime(true);
$response = $next($request);
$duration = microtime(true) - $start;
if ($duration > 1.0) { // 1秒以上かかるリクエスト
Log::warning('Slow request detected', [
'url' => $request->url(),
'duration' => $duration,
'user' => auth()->id(),
]);
}
return $response;
}
}
メンテナンスのベストプラクティス
- 定期的な健全性チェック
- データベースのインデックス最適化
- キャッシュの有効性確認
- ログファイルのローテーション
- セッションデータのクリーンアップ
- セキュリティ対策
- 依存パッケージの脆弱性チェック
- アクセスログの監視
- 認証試行の監視
- バックアップの完全性確認
- ドキュメンテーション
- API仕様書の更新
- 設定変更履歴の記録
- トラブルシューティングガイドの整備
- 運用手順書の更新
これらの対策と手順を適切に実施することで、Filamentを使用した管理画面の安定した運用が可能となります。