How to Call Middleware from Controller in Laravel?

By Hardik Savani April 16, 2024 Category : Laravel

Do you need to call middleware from controller function in laravel app, if yes then i will show you simple example to run middleware from controller constructor using "$this->middleware".

you can also define middleware in controller file in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 app.

Actually, i was working on my laravel 5.7 project. I need acl and i used entrust package for role and permission on my project. So i created products module with resource route. I require to add permission for each route. I thought if i add permission like for product create, product edit, product delete then i have to create several routes instead of resource route. But i found way to use middleware in controller method.

So, you can also use middleware in controller like as bellow example:

Example:

<?php

namespace App\Http\Controllers;

use App\Product;

use Illuminate\Http\Request;

class ProductController extends Controller

{

/**

* Create a new controller instance.

*

* @return void

*/

public function __construct()

{

$this->middleware('product-list', ['only' => ['index']]);

$this->middleware('product-create', ['only' => ['index','create','store']]);

$this->middleware('product-update', ['only' => ['index','edit','update']]);

$this->middleware('product-delete', ['only' => ['index','delete']]);

}

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$products = Product::latest()->paginate(5);

return view('products.index',compact('products'))

->with('i', (request()->input('page', 1) - 1) * 5);

}

/**

* Show the form for creating a new resource.

*

* @return \Illuminate\Http\Response

*/

public function create()

{

return view('products.create');

}

/**

* Store a newly created resource in storage.

*

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

*/

public function store(Request $request)

{

$request->validate([

'name' => 'required',

'detail' => 'required',

]);

Product::create($request->all());

return redirect()->route('products.index')

->with('success','Product created successfully.');

}

/**

* Display the specified resource.

*

* @param \App\Product $product

* @return \Illuminate\Http\Response

*/

public function show(Product $product)

{

return view('products.show',compact('product'));

}

/**

* Show the form for editing the specified resource.

*

* @param \App\Product $product

* @return \Illuminate\Http\Response

*/

public function edit(Product $product)

{

return view('products.edit',compact('product'));

}

/**

* Update the specified resource in storage.

*

* @param \Illuminate\Http\Request $request

* @param \App\Product $product

* @return \Illuminate\Http\Response

*/

public function update(Request $request, Product $product)

{

$request->validate([

'name' => 'required',

'detail' => 'required',

]);

$product->update($request->all());

return redirect()->route('products.index')

->with('success','Product updated successfully');

}

/**

* Remove the specified resource from storage.

*

* @param \App\Product $product

* @return \Illuminate\Http\Response

*/

public function destroy(Product $product)

{

$product->delete();

return redirect()->route('products.index')

->with('success','Product deleted successfully');

}

}

I hope it can help you...

Shares