Controller Middleware
Controller Middleware
MIDDLEWARE
Akash Pundir
Assistant Professor
System Programming -I
• Middleware in Laravel is a powerful
mechanism for filtering HTTP requests
entering your application. Middleware
acts as a bridge or filter between the
incoming request and your application's
routes or controllers. It can perform
various tasks like authentication,
authorization, logging, modifying the
request or response, and more.
Creating Middleware
Route::prefix('admin')->group(function () {
// Routes defined here will have the 'admin' prefix.
Route::get('dashboard', [HomeController::class,
'dashboard']);
Route::get('users', [HomeController::class, 'users']);
Route::get('settings', [HomeController::class,
'settings']);
});
Applying Miidleware to a group
Route::prefix('admin')-
>middleware('newMiddleware')->group(function ()
{
// Routes defined here will have the 'admin'
prefix and use the 'newMiddleware' middleware.
Route::get('dashboard',
[HomeController::class,'dashboard']);
Route::get('users',
[HomeController::class,'users']);
Route::get('settings',
[HomeController::class,'settings']);
});
Restful Controller
• In Laravel, a RESTful resource controller is a
controller that provides a set of predefined
methods for handling common CRUD (Create,
Read, Update, Delete) operations on a
resource. Resources are typically associated
with database tables or Eloquent models, and
RESTful resource controllers help you create
routes and controller methods for managing
these resources efficiently.
Laravel makes everything easy!!
• Command for making Restful Resource Controller
• Route::resource('posts', PostController::class);
• This single line of code generates multiple routes for common CRUD
operations, including: