5 Kỹ Thuật Để Refactor Fat Controller trong Laravel

· 3 min read

"Fat Controller" là code smell phổ biến nhất trong các ứng dụng Laravel. Nó xảy ra khi bạn đổ validation, business logic, database query, và format response tất cả vào phương thức store hoặc update.

Đây là 5 kỹ thuật để giảm cân cho controller của bạn.

1. Tách Validation Vào Form Request

Trước:

public function store(Request $request) {
    $request->validate([
        'title' => 'required|max:255',
        'body' => 'required',
    ]);
    // ...
}

Sau:

php artisan make:request StorePostRequest
public function store(StorePostRequest $request) {
    // Validation đã được thực hiện tự động ở đây
    // ...
}

2. Giới Thiệu Action Class (Single Responsibility)

Nếu một service class trở thành "God Class" xử lý mọi thứ về User, hãy thử Action Class. Một Action làm một việc.

CreatePostAction.php

class CreatePostAction {
    public function execute(User $author, array $data): Post {
        return $author->posts()->create($data);
    }
}

Controller:

public function store(StorePostRequest $request, CreatePostAction $action) {
    $post = $action->execute(auth()->user(), $request->validated());
    return redirect()->route('posts.show', $post);
}

3. Sử Dụng Model Scope và Accessor

Di chuyển logic query về Model.

Trước:

$activeUsers = User::where('active', 1)
                   ->where('last_login', '>', now()->subDays(30))
                   ->orderBy('name')
                   ->get();

Sau (User.php):

public function scopeActive($query) {
    return $query->where('active', 1);
}
// v.v...

Controller:

$users = User::active()->recent()->sorted()->get();

4. Chuyển Side Effect Sang Event/Job

Gửi email, tối ưu hình ảnh, hoặc thông báo Slack không nên block response của người dùng.

Trước:

$user->save();
Mail::to($user)->send(new WelcomeEmail()); // Chậm!

Sau:

$user->save();
WelcomeNewUserJob::dispatch($user); // Nhanh!

5. View Composer / View Model

Nếu phương thức show của bạn chuẩn bị 10 biến khác nhau cho dropdown, chart, và sidebar, di chuyển logic đó sang View Composer hoặc View Model.

Controller:

public function create() {
    return view('posts.create'); 
    // Tất cả dữ liệu dropdown được inject qua app/View/Composers/PostComposer.php
}

Tóm Tắt

Công việc của controller là:

  1. Nhận request.
  2. Chuyển đến handler logic đúng (Service/Action).
  3. Trả về response.

Nó KHÔNG nên biết cách lưu user hoặc cách validate dữ liệu.

Bình luận