😒Cách tự tạo một middleware cho phép đăng nhập đường dẫn, Authentication (ok)
Chú ý đây là dự án shop ban han laravel cua hieu truong.
Last updated
Chú ý đây là dự án shop ban han laravel cua hieu truong.
Last updated
app\Http\Middleware\AccessPermission.php
<?php
namespace App\Http\Middleware;
use Closure;
use Auth;
use App\Models\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
class AccessPermission
{
protected $admin;
public function __construct(Admin $admin) {
$this->admin = $admin;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
$actions = Route::getCurrentRoute()->getAction();
$roles = isset($actions['auth.users']) ? $actions['auth.users'] : null;
if ($this->admin->hasRole($roles) || !$roles) {
return $next($request);
} else {
return redirect('admin');
}
}
}
app\Models\Admin.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Admin extends Authenticatable
{
use HasFactory, Notifiable;
public $timestamp = false;
protected $table = 'tbl_admin';
protected $primaryKey = 'admin_id';
protected $fillable = [
'admin_email',
'admin_password',
'admin_name',
'admin_phone'
];
public function getAuthPassword(){
return $this->admin_password;
}
public function roles()
{
return $this->belongsToMany(Role::class, 'admin_roles');
}
public function hasRole($role){
if($this->roles()->where('name',$role)->first()){
return true;
}
return false;
}
public function hasAnyRoles($roles){
if(is_array($roles)){
foreach($roles as $role){
if($this->hasRole($role)){
return true;
}
}
}else{
if($this->hasRole($roles)){
return true;
}
}
return false;
}
}
app\Http\Kernel.php
protected $routeMiddleware = [
// ...p
'auth.users' => \App\Http\Middleware\AccessPermission::class,
];
routes\web.php
Route::group(['middleware' => 'auth.users', 'auth.users'=>['admin','author']], function () {
Route::get('/add-users',[UserController::class, 'add_users']);
});
resources\views\admin\users\add_users.blade.php
@extends('../layouts.admin_layout')
@section('sidebar-start')
@include('admin.sidebar-start')
@endsection
@section('content')
<div class="row">
<div class="col-lg-12">
<section class="panel">
<header class="panel-heading">
Thêm user
</header>
<?php
$message = Session::get('message');
if($message){
echo '<span class="text-alert">'.$message.'</span>';
Session::put('message',null);
}
?>
<div class="panel-body">
<div class="position-center">
<form role="form" action="{{URL::to('store-users')}}" method="post">
{{ csrf_field() }}
<div class="form-group">
<label for="exampleInputEmail1">Tên users</label>
<input type="text" name="admin_name" class="form-control" id="exampleInputEmail1" placeholder="Tên danh mục">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email</label>
<input type="text" name="admin_email" class="form-control" id="exampleInputEmail1" placeholder="Slug">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Phone</label>
<input type="text" name="admin_phone" class="form-control" id="exampleInputEmail1" placeholder="Slug">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Password</label>
<input type="text" name="admin_password" class="form-control" id="exampleInputEmail1" placeholder="Slug">
</div>
<button type="submit" name="add_category_product" class="btn btn-info">Thêm users</button>
</form>
</div>
</div>
</section>
</div>
@endsection
app\Http\Controllers\UserController.php
public function add_users(){
lreturn view('admin.users.add_users');
}