0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Laravel における命名規則

Posted at

Laravel における Controller 名、Blade 名、Blade ファイル名の命名ルール

Laravel のプロジェクトで Controller / Blade / Bladeファイル の命名を統一すると、
可読性が向上し、チーム開発でも一貫したコード管理が可能になる。

1. Controller の命名ルール

基本ルール

単数形 + Controller をつける
PascalCase(頭文字を大文字にする)を使う
リソースコントローラなら RESTful に準拠した命名をする

具体例
【用途】	    【Controller名】	   【説明】

ユーザー管理	  UserController	     ユーザーの CRUD 処理を担当
記事管理	    ArticleController 	 記事の CRUD 処理を担当
認証処理	      AuthController	     認証関連の処理(ログイン・ログアウト)
ダッシュボード	  DashboardController	 ダッシュボード画面の処理
プロフィール	  ProfileController	     ユーザーのプロフィール画面の処理

コントローラーの生成コマンド

example.php
php artisan make:controller UserController

2. Blade(ビュー)の命名ルール

基本ルール

• スネークケースを使用する場合は(例snake_case.blade.php) で記述
• 関連するビューは resources/views/下のサブディレクトリに整理する
• Controller のメソッドに対応するファイル名をつけるを推奨する

Blade記述具体例
【画面の用途】  【Blade名】	Blade ファイル名
ユーザー一覧	  users.index	 resources/views/users/index.blade.php
ユーザー詳細 	  users.show	 resources/views/users/show.blade.php
ユーザー編集	  users.edit	 resources/views/users/edit.blade.php
記事一覧	      articles.index resources/views/articles/index.blade.php
記事詳細	      articles.show	 resources/views/articles/show.blade.php
ログイン画面	  auth.login	 resources/views/auth/login.blade.php

3. Blade ファイルのフォルダ構成

フォルダ構成
resources/views/
├── layouts/
│   ├── app.blade.php  # 共通レイアウト
│   ├── header.blade.php  # ヘッダー
│   ├── footer.blade.php  # フッター
│
├── users/
│   ├── index.blade.php  # ユーザー一覧
│   ├── show.blade.php  # ユーザー詳細
│   ├── edit.blade.php  # ユーザー編集
│
├── articles/
│   ├── index.blade.php  # 記事一覧
│   ├── show.blade.php  # 記事詳細
│
└── auth/
    ├── login.blade.php  # ログイン
    ├── register.blade.php  # 会員登録

•各フォルダ毎にリソースを整理すると管理しやすくなる

4. Controller と Blade の関連付け

•UserController.php(コントローラ)の例

example.php
class UserController extends Controller
{
    // ユーザー一覧
    public function index()
    {
        return view('users.index');
    }

    // ユーザー詳細
    public function show($id)
    {
        return view('users.show', compact('id'));
    }

    // ユーザー編集
    public function edit($id)
    {
        return view('users.edit', compact('id'));
    }
}

5. Blade の記述方法

resources/views/users/index.blade.php(ユーザー一覧のビュー)
記述例→ @extends('layouts.app') で layouts/app.blade.php を継承している

example.php
@extends('layouts.app')

@section('content')
    <h1>ユーザー一覧</h1>
    <ul>
        @foreach($users as $user)
            <li>{{ $user->name }}</li>
        @endforeach
    </ul>
@endsection

6. この記事のまとめ

以下の命名規則を適用すると、Laravelでのプロジェクトの可読性・管理のしやすさが向上する

•Controller は PascalCase + Controller(例: UserController)
•Blade(ビュー)は snake_case.blade.php で命名(例: index.blade.php)
•ビューは resources/views/ 以下にフォルダを作成して整理

•Controller の return view('users.index');で Bladeと連携
•共通レイアウトは layouts/ に格納して @extends で継承

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?