Laravel Controllers

Controllers are the most important feature in MVC architecture. In MVC, 'C' stands for controller.

When we create the controller using artisan command, it'll be saved under app/http/Controllers directory.

# Basic Controllers

Controller can be created by executing the following artisan command -

php artisan make:controller controller_name

We'll create controller i.e UserController using artisan command.

php artisan make:middleware UserController

it will be created under app/Http/Controllers/ directory.

<?php 
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

class UserController extends Controller 
 {
  // Do Stuff
 }

# Resource Controllers

Laravel resource controller makes it easier to create CRUD application.

<?php 
namespace App\Http\Controllers;
use Illuminate\Http\Request;

class UserController extends Controller 
 {
   public function index()
   {
      // Display a listing of the resource.
   }
   public function create()
   {
      // Show the form for creating a new resource.
   }
   public function store(Request $request)
   {
      // Store a newly created resource in storage.
   }
   public function show($id)
   {
      // Display the specified resource.
   }
   public function edit($id)
   {
      // Show the form for editing the specified resource.
   }
   public function update(Request $request, $id)
   {
      // Update the specified resource in storage.
   }
   public function destroy($id)
   {
      // Remove the specified resource from storage.
   }
}

 Route::resource('user', 'UserController');

Laravel Resource Routing