Laravel Controller

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.


Creating a Controller

You can create a controller using Artisan:

php artisan make:controller UserController


This will create app/Http/Controllers/UserController.php.


Basic Controller Route

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:


Single-Action Controllers

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:


Controller with Parameters

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;
}



Named Controller Routes

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]);

Controller Route Groups

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

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


API Resource Controllers

For API-only routes (no create, edit):

php artisan make:controller ProductController --resource

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);

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.