🚀
Laravel コマンドまとめ
コントローラー (Controllers)
基本コマンド: php artisan make:controller [名前]Controller
命名規則
- 必ず「単語」+「Controller」をつける
- PascalCase(各単語の先頭を大文字)を使用
- 単数形を使用
# 基本的な例
php artisan make:controller UserController
php artisan make:controller ProductController
php artisan make:controller OrderController
# サブディレクトリを使用する場合
php artisan make:controller Admin/UserController
php artisan make:controller Api/ProductController
# 具体的な機能を表す場合
php artisan make:controller UserProfileController
php artisan make:controller OrderHistoryController
# ❌ 誤った例
php artisan make:controller user # Controllerがない
php artisan make:controller UserControllers # 複数形
php artisan make:controller userController # 小文字始まり
モデル (Models)
基本コマンド: php artisan make:model [名前]
命名規則
- 単数形で記述
- PascalCase(各単語の先頭を大文字)を使用
- Model接尾辞は不要
# 基本的な例
php artisan make:model User
php artisan make:model Product
php artisan make:model OrderItem
# よく使う組み合わせオプション
-m, --migration # マイグレーションを作成
-c, --controller # コントローラーを作成
-f, --factory # ファクトリーを作成
-s, --seed # シーダーを作成
-r, --resource # リソースコントローラーを作成
# よくある組み合わせ例
php artisan make:model User -m # モデルとマイグレーション
php artisan make:model User -mc # モデル、マイグレーション、コントローラー
php artisan make:model User -mcf # モデル、マイグレーション、コントローラー、ファクトリー
php artisan make:model User --all # すべて作成(-aでも可)
# サブディレクトリを使用する場合
php artisan make:model Admin/Role
php artisan make:model Shop/Product
# ❌ 誤った例
php artisan make:model users # 複数形
php artisan make:model userModel # 小文字始まり、Modelは不要
php artisan make:model user_profile # スネークケース
テスト (Tests)
基本コマンド: php artisan make:test [名前]Test
命名規則
- 必ず「単語」+「Test」をつける
- PascalCase(各単語の先頭を大文字)を使用
- テストの目的を明確に示す名前にする
# 基本的な例
php artisan make:test UserTest
php artisan make:test ProductTest
# 具体的な機能テスト
php artisan make:test UserRegistrationTest
php artisan make:test OrderProcessTest
php artisan make:test ProductPurchaseTest
# ユニットテスト
php artisan make:test UserTest --unit
php artisan make:test Services/PaymentServiceTest --unit
# ❌ 誤った例
php artisan make:test user # Testがない
php artisan make:test UserTests # 複数形
php artisan make:test userTest # 小文字始まり
その他のクラス
ミドルウェア (Middleware)
php artisan make:middleware CheckAdmin # CheckAdminMiddlewareが生成される
サービスプロバイダー (Service Providers)
php artisan make:provider PaymentServiceProvider
ジョブ (Jobs)
php artisan make:job ProcessPodcast
命名規則の一般的な原則
-
PascalCase の使用
- クラス名は必ずPascalCase
- 各単語の先頭を大文字にする
-
意味のある名前
- クラスの役割や目的が明確に分かる名前をつける
- 抽象的な名前は避ける
-
単数形の使用
- モデルは必ず単数形
- コントローラーのベース名も単数形
-
接尾辞の適切な使用
- Controller: 必須
- Test: 必須
- Model: 不要
- Middleware: 自動付加
- Provider: 必須
ディレクトリ構造例
機能や役割に応じて適切にディレクトリを分割する場合:
# API関連
php artisan make:controller Api/V1/UserController
php artisan make:controller Api/V2/UserController
# 管理者関連
php artisan make:controller Admin/UserController
php artisan make:test Admin/UserManagementTest
# サービス層のテスト
php artisan make:test Services/PaymentServiceTest --unit
php artisan make:test Services/OrderProcessingTest --unit
Discussion