CONTROLLER
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
• php artisan make:middleware
MiddlewareName
Controller Middleware
• Middleware may be assigned to the controller's routes in
your route files:
In Kernel.php of http directory
Route::get('profile', [UserController::class, 'show'])-
>middleware(‘newMiddleware');
Controller Middleware(contd.)
• Or, you may find it convenient to specify middleware
within your controller's constructor.
• Using the middleware method within your controller's
constructor, you can assign middleware to the controller's
actions:
Controller Middleware(contd.)
class UserController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('log')->only('index');
$this->middleware('subscribed')->except('store');
}
}
Simple Grouping of Route based on prefix
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
• php artisan make:controller PostController --resource
Defining Route for controller:
• In your routes/web.php file, you can define resourceful routes using
the Route::resource() method. Here's an example:
• Route::resource('posts', PostController::class);
• This single line of code generates multiple routes for common CRUD
operations, including:
• GET /posts (index method): Display a list of all posts.
• GET /posts/create (create method): Show the form for creating a new post.
• POST /posts (store method): Store a newly created post in the database.
• GET /posts/{id} (show method): Display the details of a specific post.
• GET /posts/{id}/edit (edit method): Show the form for editing a post.
• PUT/PATCH /posts/{id} (update method): Update a specific post in the
database.
• DELETE /posts/{id} (destroy method): Delete a specific post from the
database.