Laravel 5.5 tutorial for beginners : This lessons will show you how to customize default Laravel Authentication using Middleware that can login with multiple level access.
Register middleware in app\Http\Kernel.php we can register the IsAdmin middleware
create new page in resources/view/ and named with "adminhome.blade.php"
See you next lessons and happy coding guys ...
Video tutorial Laravel 5.5 Middleware
Full source code
Create new Laravel Project using composer
composer create-project --prefer-dist laravel/laravel multiauth
User Migration
update user migration to add new field named with "admin"
<?php
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->boolean('admin')->default(0);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
User Model
update the user model
<?php
namespace App;
use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password','admin',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
Run Migration
php artisan migrate
Create Laravel Authentication
php artisan make:auth
Create New Middleware
php artisan make:middleware IsAdmin
Register middleware in app\Http\Kernel.php we can register the IsAdmin middleware
<?php
namespace AppHttp;
use IlluminateFoundationHttpKernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode::class,
IlluminateFoundationHttpMiddlewareValidatePostSize::class,
AppHttpMiddlewareTrimStrings::class,
IlluminateFoundationHttpMiddlewareConvertEmptyStringsToNull::class,
AppHttpMiddlewareTrustProxies::class,
AppHttpMiddlewareIsAdmin::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
AppHttpMiddlewareEncryptCookies::class,
IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
IlluminateSessionMiddlewareStartSession::class,
// IlluminateSessionMiddlewareAuthenticateSession::class,
IlluminateViewMiddlewareShareErrorsFromSession::class,
AppHttpMiddlewareVerifyCsrfToken::class,
IlluminateRoutingMiddlewareSubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => IlluminateAuthMiddlewareAuthenticate::class,
'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,
'bindings' => IlluminateRoutingMiddlewareSubstituteBindings::class,
'can' => IlluminateAuthMiddlewareAuthorize::class,
'guest' => AppHttpMiddlewareRedirectIfAuthenticated::class,
'throttle' => IlluminateRoutingMiddlewareThrottleRequests::class,
];
}
Routes (web.php)
<?php
Auth::routes();
Route::group(['middleware' => ['web','auth']], function(){
Route::get('/', function () {
return view('welcome');
});
Route::get('/home', function() {
if (Auth::user()->admin == 0) {
return view('home');
} else {
$users['users'] = AppUser::all();
return view('adminhome', $users);
}
});
});
Create new Admin Page
If the users is "Admin", we will redirect to admin home pages, and if just the user will redirect into home pagescreate new page in resources/view/ and named with "adminhome.blade.php"
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Dashboard</div>
<div class="panel-body">
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
<div class="alert alert-success">
<p>You're logged in as ADMINISTRATOR</p>
</div>
<table class="table table-hover table-bordered">
<thead>
<tr>
<th width="5">No.</th>
<th>Member Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach ($users as $key => $value)
<tr>
<td>{{ $key+1 }}</td>
<td>{{ $value->name }}</td>
<td>{{ $value->email }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
@endsection
Home.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Dashboard</div>
<div class="panel-body">
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
<div class="alert alert-success">
<p>You're logged in as USERS</p>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
More Laravel Video Tutorial
See you next lessons and happy coding guys ...
Không có nhận xét nào:
Đăng nhận xét