0% found this document useful (0 votes)
12 views

Laravel Part-9

Uploaded by

waver58650
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Laravel Part-9

Uploaded by

waver58650
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Video-29 (Middleware in Laravel):-

----------------------------------------------
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);
}
}

Register the middleware in Kernel file : -app/Http/Kernal.php


<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel


{
protected $middleware = [
\App\Http\Middleware\UnderConstruction::class,

];
}

a. If we customise this page then we create a file inside views : -


b. For creating a file we need to create a folder error inside the views
folder.
c. Location of customised views:- resources\views\errors\503.blade.php
d. Default error page create in laravel and location of that file is :
php artisan vendor:publish --tag=laravel-errors
Location:- resources\views\errors\Files.blade.php

2. Assigning Middleware to Routes


a. If you would like to assign middleware to specific routes,You have to
add your own middleware to list and assign it a key of your choosing
in the $routeMiddleware property of your application’s
app/Http/Kernel.php.
b. Once the middleware has been defined in the HTTP kernel, you may
use the middleware method to assign middleware to a route:
Route::get('/profile', function () {
//
})->middleware('auth');

c. You may assign multiple middleware to the route by passing an array


of middleware names to the middleware method:
Route::get('/', function () {
//
})->middleware(['first', 'second']);

Example: -
Location of Controller : - app/Http/Controller/ReportController
php artisan make:controller ReportController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

class ReportController extends Controller


{
public function show()
{
return view('report');
}
}

Location of web.php :- Route/web.php


<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ReportController;

Route::get('report',[ReportController::class,'show'])->middleware
(‘construction’);

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);
}
}

Register the middleware in Kernel file : -app/Http/Kernal.php


<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel


{
protected $routeMiddleware = [
'construction'=>\App\Http\Middleware\UnderConstruction::class,
];
}

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;

class ReportController extends Controller


{
public function show()
{
return view('report');
}
}

Location of web.php :- Route/web.php


<?php

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’);

// 2nd way to use middleware using group function


Route::middleware(['construction'])->group(function(){
Route::get('stock',function(){
return view('stock');
});

Route::get('report',[ReportController::class,'show']);
});

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);
}
}

Register the middleware in Kernel file : -app/Http/Kernal.php


<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel


{
protected $middlewareGroups = [
'construction'=>[
\App\Http\Middleware\UnderConstruction::class,
],
];
}

Passing Additional data to Middleware


Location of Controller : - app/Http/Controller/ReportController
php artisan make:controller ReportController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

class ReportController extends Controller


{
public function show()
{
return view('report');
}
}
Location of web.php :- Route/web.php
<?php

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']);

Location of middleware:- app/middleware/CheckRole


php artisan make:middleware CheckRole
<?php

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);
}
}

Register the middleware in Kernel file : -app/Http/Kernal.php


<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel


{
protected $middlewareGroups = [
'role'=>[
\App\Http\Middleware\CheckRole::class,
],
];
}

Video-30 (Creating form Accessing form data in Laravel):-


-------------------------------------------------------------------------------
There are three things to care in form : -
1. Creating form
2. Accessing form data
3. Validation form

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.

2. Accessing Form Data


a. Create a controller : app/Http/Controller/Controller_Name
php artisan make:controller RegistrationController
public function signup(Request $request)
{
// dd($request);
return view('welcome');
}

b. What we will do inside this Controller and blade file : -


i. Accessing all input Data : -Using all method
public function signup(Request $request)
{
// Using all method
print_r($request->all());
return view('welcome');
}

ii. Accessing all input Data as an associative array


public function signup(Request $request)
{
// Using input method
print_r($request->input());
return view('welcome');
}

iii. Accessing an individual input data


public function signup(Request $request)
{
// Using input method inside pass the value
print_r($request->input('name'));
print_r($request->input('email'));
print_r($request->input('pass'));
return view('welcome');
}

iv. Accessing input via dynamic properties


public function signup(Request $request)
{
//Accessing dynamic value
print_r($request->name);
print_r($request->email);
print_r($request->pass);
return view('welcome');
}

v. Determining if input is present


public function signup(Request $request)
{
if($request->has('name'))
{
print_r($request->input('name'));
}
}

vi. Multiple determining if input is present


public function signup(Request $request)
{
if($request->has(['name',’email’]))
{
print_r($request->input('name'));
print_r($request->input(‘email'));
}
}

vii.Accessing a portion of input data


public function signup(Request $request)
{
if($request->hasAny(['name','email']))
{
print_r($request->input('name'));
print_r($request->input('email'));
}
}

public function signup(Request $request)


{
if($request->filled('name')
{
print_r($request->input('name'));
}
}

public function signup(Request $request)


{
if($request->missing(‘username’)
{
print_r(‘inside missing code not present’);
}
}
public function signup(Request $request)
{
// closer function
$request->whenHas('name',function($input){
print_r('Data modify');
});
}

public function signup(Request $request)


{
// closer function
$request->whenFilled('name',function($input){
print_r('Data modify');
});
}

viii. Flashing input to the session :- If form submitted then request is


stored in session,usko kaise flashing kiya jata hai ?
public function signup(Request $request)
{
// stored data in session for next request
$request->flash();
}

public function signup(Request $request)


{
// stored data in session for next request for specific data
$request->flashOnly([‘name’,’email’]);
}

public function signup(Request $request)


{
// stored data in session for next request
$request->flashExcept(‘pass’);
}

ix. Accessing old data


public function signup(Request $request)
{
// stored data in session for next request
$request->flash();
print_r($request->old('name'));
print_r($request->old('email'));
print_r($request->old('pass'));
}

x. Flashing input then redirecting

public function signup(Request $request)


{
return redirect('olddata')->withInput();
}

public function signup(Request $request)


{
// Named Route ke through
return redirect()->route('old')->withInput();
}

public function signup(Request $request)


{
// Except any filled with old value reserved
return redirect(‘olddata)->withInput(
$request->except(‘password’)
);
}

You might also like