Laravel Controllers


The controller is a part of the MVC pattern. it is the communicator between Views and Models. where we can write logic code.

Laravel Create Controller

Now, open the command prompt and go to your project directory using the command prompt. Enter following the below PHP artisan command to create the laravel controller.

Syntax

php artisan make:controller <controller-name>

Example

php artisan make:controller UserController

after running the above command, The UserController.php file will be created in the “app/Http/Controllers” directory.

app/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;

class UserController extends Controller {
   //
}

?>

Laravel Controller Middleware

The laravel Middleware Controller can also be assigned to the controller route in our route. so we can also restrict the method or controller using middleware.

assign middleware to route

Route::get(‘user’, ‘UserController@index’)->middleware(‘auth’);

Middleware in constructor laravel

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;

class UserController extends Controller {
   public function __construct() {
      $this->middleware('auth');
   }
}

Laravel Resource Controllers

The laravel Resource Controller can be easily created the CRUD (Create, Read, Update, Delete) operations. so we can easily create the resource controller using the below command.

php artisan make:controller UserController --resource

after run the above command, The UserController.php file will be created with some method (like as index, create, store, show, edit, update and destroy)in the “app/Http/Controllers” directory. so you can see below example code.

<?php

namespace App\Http\Controllers;
use App\Http\Requests;

class UserController extends Controller
{
   /**
    * Display a listing of the resource.
    *
    * @return Response
    */
   public function index()
   {
       //
   }

   /**
    * Show the form for creating a new resource.
    *
    * @return Response
    */
   public function create()
   {
       //
   }

   /**
    * Store a newly created resource in storage.
    *
    * @param  Request  $request
    * @return Response
    */
   public function store(Request $request)
   {
       //
   }

   /**
    * Display the specified resource.
    *
    * @param  int  $id
    * @return Response
    */
   public function show($id)
   {
       //
   }

   /**
    * Show the form for editing the specified resource.
    *
    * @param  int  $id
    * @return Response
    */
   public function edit($id)
   {
       //
   }

   /**
    * Update the specified resource in storage.
    *
    * @param  Request  $request
    * @param  int  $id
    * @return Response
    */
   public function update(Request $request, $id)
   {
       //
   }

   /**
    * Remove the specified resource from storage.
    *
    * @param  int  $id
    * @return Response
    */
   public function destroy($id)
   {
       //
   }
}
?>