DekGenius.com
PHP
create middleware in laravel
php artisan make:middleware nameOfMiddleware
laravel make:middleware
php artisan make:middleware AdminMiddleware
laravel middleware in constructor
public function __construct(User $user)
{
$this->user = $user;
$this->middleware(function ($request, $next) {
$user = auth()->user();
if ($user) {
$this->user = $user;
}
return $next($request);
});
}
laravel make:middleware
Route::group(['middleware' => 'AppHttpMiddlewareAdminMiddleware'], function()
{
Route::get ('/admin', ['uses' => 'AdminController@index', 'before' => 'admin']);
});
middleware command in laravel
php artisan make:middleware NameOfTheMiddleware
laravel middleware
php artisan make:middleware EnsureTokenIsValid
command to create middleware in laravel
php artisan make:middleware MiddlewreName
laravel middleware
Route::get('/profile', function () {
//
})->middleware('auth');
create middleware laravel
php artisan make:middleware <middleware-name>
laravel make:middleware
public function handle($request, Closure $next)
{
if ($request->user()->type != 'A')
{
return redirect('home');
}
return $next($request);
}
middleware in laravel
<?php
namespace AppHttpMiddleware;
use Closure;
class BeforeMiddleware
{
public function handle($request, Closure $next)
{
// Perform action
return $next($request);
}
}
laravel make:middleware
if (Auth::user()->is_admin != 1) {...}
laravel set middleware default
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.
waht is middleware in laravel
The middleware in web applications is the mid-layer between the HTTP request and the application logic. The middleware process incoming requests and execute the code before the controller's actions.
custom middleware laravel 8
<?php
namespace AppHttpMiddleware;
use Closure;
class EnsureUserHasRole
{
/**
* Handle the incoming request.
*
* @param IlluminateHttpRequest $request
* @param Closure $next
* @param string $role
* @return mixed
*/
public function handle($request, Closure $next, $role)
{
if (! $request->user()->hasRole($role)) {
// Redirect...
}
return $next($request);
}
}
custom middleware laravel 8
Route::put('/post/{id}', function ($id) {
//
})->middleware('role:editor');
middleware in laravel
<?php
namespace AppHttpMiddleware;
use Closure;
class CheckType
{
public function handle($request, Closure $next)
{
// Perform action
return $next($request);
}
}
© 2022 Copyright:
DekGenius.com