C:\xampp\htdocs Chạy cmd chạy lệnh composer create-project laravel/laravel gate
Chờ dowload xong mở dự án lên trong PhpStorm.
- Vào Navicat tạo mơi một database gate
- Vào phpStorm vào file .env
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:gj4xflC+EyxtJ0SfrdjDsmdzyeQpPVo8e5SztO2drUM=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=gate
DB_USERNAME=root
DB_PASSWORD=
- Vào terminal chạy
composer require laravel/ui:^2.4
php artisan ui vue --auth
npm install && npm run dev
- Vào AppServiceProvider.php
public function boot()
{
Schema::defaultStringLength(191);
}
- Vào terminal chạy lệnh : php artisan migrate
- Lên trình duyệt tạo tài khoản:
- Sau khi tạo xong :- Tạo tiếp 1 tài khoản
- Sau khi tạo xong :
- Sau khi tạo xong :
- Vào web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/admin', 'HomeController@admin')->name('admin');
- Vào HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home');
}
public function admin()
{
return view('admin');
}
}
- Vào resource => views => layouts => Tạo file admin.blade.php
<h2>Đây là page Admin</h2>
- Vào AuthServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
// 'App\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Gate::define('is_admin', function ( $user) {
return $user->is_admin;
});
}
}
- Vào Navicat Vào bảng user
<?php- Vào home.blade.php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/admin', 'HomeController@admin')->name('admin');
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Dashboard') }}</div>
<div class="card-body">
@if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
{{ __('You are logged in!') }}
@can('is-admin')
<a href="{{route('admin')}}">Admin</a>
@endcan
</div>
</div>
</div>
</div>
</div>
@endsection
- Vào HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home');
}
public function admin()
{
if(Gate::allows('is-admin'))
{
return view('admin');
}
else{
abort(403);
}
}
}
- Vào admin.blade.php
<h2>Đây là page Admin</h2>
- Vào resoucre => views => tạo foder errors => tạo file 433.blade.php
<h2>Bạn không được phép truy cập</h2>
- Vào AuthServiceProvider
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
// 'App\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Gate::define('is-admin', function ($user) {
return $user->is_admin;
});
}
}
Kiến thức cơ bản về policy
link:https://www.youtube.com/watch?v=pbUzlzXvbGY&list=PL3V6a6RU5ogEAKIuGjfPEJ77FGmEAQXTT&index=59
0 Nhận xét