How to Call Middleware from Controller in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to use Carbon in Laravel Blade or Controller File?
- How to Call Controller Function in Blade Laravel?
- Laravel Redirect to Route from Controller Example
- How to Call a Controller Function in Another Controller in Laravel?
- Laravel Call Function from Same Controller Example
- Laravel - How to Get .env Variable in Blade or Controller?
- How to Create Resource Controller in Laravel?
- Laravel User Access Control using Middleware Example
- How to Execute Artisan Command from Controller in Laravel?
- How to Get Current Controller Name in View Laravel?
- Laravel Get Route Parameters in Middleware Example
- Laravel XSS Protection Middleware Example