Laravel Basic
Laravel Form
Laravel Database
Laravel Advance
Laravel controllers are used to group related route logic into a single class. Instead of writing closures directly in routes, controllers provide a clean structure for handling web requests.
You can create a controller using Artisan:
php artisan make:controller UserController
This will create app/Http/Controllers/UserController.php.
Map a route to a controller method:
web.php(routes\web.php):
use App\Http\Controllers\UserController;
Route::get('/users', [UserController::class, 'index']);
UserController.php(app\Http\Controllers\UserController.php):
class UserController extends Controller
{
public function index() {
return "List of users";
}
}
Output:
If your controller handles only one action, you can use __invoke:
php artisan make:controller ProfileController --invokable
ProfileController.php
class ProfileController extends Controller
{
public function __invoke(Request $request)
{
return "List of profiles";
}
}
Route:
use App\Http\Controllers\ProfileController;
Route::get('/profiles', ProfileController::class);
Output:
Pass URL parameters to controller methods:
web.php(routes\web.php):
Route::get('/user/{id}', [UserController::class, 'show']);
UserController.php(app\Http\Controllers\UserController.php):
public function show($id) {
return "User ID: " . $id;
}
Assign a name to controller routes:
web.php(routes\web.php):
Route::get('/user/{id}', [UserController::class, 'show'])->name('user.show');
$url = route('user.show', ['id' => 5]);
Group routes for the same controller:
Route::controller(UserController::class)->group(function () {
Route::get('/users', 'index');
Route::get('/users/{id}', 'show');
Route::post('/users', 'store');
});
Resource controllers provide CRUD routes automatically:
php artisan make:controller PostController --resource
This will create app/Http/Controllers/PostController.php. with index,create,store,show,edit,update,destroy functions
class PostController extends Controller
{
public function index(){ // }
public function create(){ // }
public function store(Request $request){ // }
public function show(string $id){ // }
public function edit(string $id){ // }
public function update(Request $request, string $id){ // }
public function destroy(string $id){ // }
}
web.php(routes\web.php):
Route::resource('posts', PostController::class);
Generated routes:
|
Method |
URI |
Action |
Name |
|---|---|---|---|
|
GET |
/posts |
index |
posts.index |
|
GET |
/posts/create |
create |
posts.create |
|
POST |
/posts |
store |
posts.store |
|
GET |
/posts/{post} |
show |
posts.show |
|
GET |
/posts/{post}/edit |
edit |
posts.edit |
|
PUT/PATCH |
/posts/{post} |
update |
posts.update |
|
DELETE |
/posts/{post} |
destroy |
posts.destroy |
For API-only routes (no create, edit):
php artisan make:controller ProductController --api
This will create app/Http/Controllers/ProductController.php. with index,store,show,update,destroy functions
class PostController extends Controller
{
public function index(){ // }
public function store(Request $request){ // }
public function show(string $id){ // }
public function update(Request $request, string $id){ // }
public function destroy(string $id){ // }
}
api.php(routes\api.php):
Route::apiResource('products', ProductController::class);