Laravel Part-9
Laravel Part-9
----------------------------------------------
1. Middleware provides a convenient mechanism for inspecting and
filtering HTTP requests entering your application.
2. All of these middleware are located in the app/Http/Middleware
directory.
Defining Middleware
php artisan make:middleware Middleware_Name
Registering Middleware
1. Global Middleware
2. Assigning Middleware to Routes
3. Middleware Groups
1. Global Middleware
If you want a middleware to run during every HTTP request to your
application, list the middleware class in the $middleware property of
your app/Http/Kernel.php class.
Example: -
Location of middleware:- app/middleware/UnderConstruction
php artisan make:middleware UnderConstruction
<?php
namespace App\Http\Middleware;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Closure;
use Illuminate\Http\Request;
class UnderConstruction
{
public function handle(Request $request, Closure $next)
{
throw new HttpException(503);
}
}
];
}
Example: -
Location of Controller : - app/Http/Controller/ReportController
php artisan make:controller ReportController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ReportController;
Route::get('report',[ReportController::class,'show'])->middleware
(‘construction’);
namespace App\Http\Middleware;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Closure;
use Illuminate\Http\Request;
class UnderConstruction
{
public function handle(Request $request, Closure $next)
{
throw new HttpException(503);
}
}
3. Middleware Groups
a. If you may want to group several middleware under a single key to
make them easier to assign to routes. You may accomplish this using
the $middlewareGroups property of your HTTP kernel.
Example: -
Location of Controller : - app/Http/Controller/ReportController
php artisan make:controller ReportController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ReportController;
// 1st way to use middleware
Route::get(‘stock’,function(){
return view(‘stock’);
})->middleware(‘construction’);
Route::get('report',[ReportController::class,'show'])->middleware
(‘construction’);
Route::get('report',[ReportController::class,'show']);
});
namespace App\Http\Middleware;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Closure;
use Illuminate\Http\Request;
class UnderConstruction
{
public function handle(Request $request, Closure $next)
{
throw new HttpException(503);
}
}
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ReportController;
Route::get(‘dashboard’,function(){
return view(‘dashboard’);
})->midddleware(‘role:admin’);//You are passing value admin
and admin is pass through the middleware function parameter
$role
Route::get('report',[ReportController::class,'show']);
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class CheckRole
{
public function handle(Request $request, Closure $next, $role)
{
if($role === 'guest')
{
return redirect('/');
}
return $next($request);
}
}
1. Creating Form
a. Location of form :- resources/views/registeration.blade.php
b. We need a @csrf token when we use the post method for protecting
our website hacking.
<form action="" method="post">
@csrf
Name : <input type="text" name="name" id="name"><br>
Email: <input type="email" name="email" id="email"> <br>
password : <input type="password" name="pass" id="pass"> <br>
<input type="submit" value="submit">
</form>
CSRF
a. Cross-site request forgeries are a type of malicious exploit whereby
unauthorised commands are performed on behalf of an authenticated
user. Thankfully, Laravel makes it easy to protect your application from
cross-site request forgery (CSRF) attacks.
b. You may use the @csrf Blade directive to generate the hidden token
input field.