expand_less

Controllers

Introduction

Instead of defining all your request handling logic as closures in route files, consider organizing this behavior using "controller" classes. Controllers group related request handling logic into a single class, promoting better organization and maintainability. By default, controllers are stored in the App/Controllers directory.

Writing Controllers

Basic Controllers

To quickly generate a new controller, use the following command:

                                
                                    
php dfork make:controller UserController

The created controller will automatically receive a basic structure, inheriting the Request class and extending the foundational Controller class.

Once you have written a controller class and method, you may define a route to the controller method like so:

                                
                                    
Route::get('/user', [UserController::class, 'index']);