AdminLte Link: - https://drive.google.com/drive/folders/1MNopXoRa3Qhcu2x-u5pmClBN1oVgVGi3?usp=sharing
Project Laravel: https://drive.google.com/file/d/11e0oKEnCpQq_GJxMsMgw29qoB7R5n1sc/view?usp=drive_link
- Download the Above Admin Template
- Create Admin Login Interface
- Create Gueard And Provider For Admin Authentication
- Run Migration and create a role column in the user table
- Create an Admin User Using Tinker
- Apply Form Validation and authentic the user
D:\xampp\htdocs\laravel-admin\app\Http\Middleware\AdminAuthenticate
<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class AdminAuthenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('admin.login');
}
}
protected function authenticate($request, array $guards)
{
if ($this->auth->guard('admin')->check()) {
return $this->auth->shouldUse('admin');
}
$this->unauthenticated($request,['admin']);
}
}
D:\xampp\htdocs\laravel-admin\app\Http\Middleware\AdminRedirectIfAuthenticated
<?php
namespace App\Http\Middleware;
use App\Providers\RouteServiceProvider;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AdminRedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @param string|null ...$guards
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next, ...$guards)
{
if (Auth::guard('admin')->check()) {
return redirect()->route('admin.dashboard');
}
return $next($request);
}
}
Register Middelware in Kernel
D:\xampp\htdocs\laravel-admin\app\Http\Kernel
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel 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<int, class-string|string>
*/
protected $middleware = [
// \App\Http\Middleware\TrustHosts::class,
\App\Http\Middleware\TrustProxies::class,
\Fruitcake\Cors\HandleCors::class,
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
/**
* The application's route middleware groups.
*
* @var array<string, array<int, class-string|string>>
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array<string, class-string|string>
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'admin.auth' => \App\Http\Middleware\AdminAuthenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'admin.guest' => \App\Http\Middleware\AdminRedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];
}
Define Route in D:\xampp\htdocs\laravel-admin\routes\web.php
<?php
use App\Http\Controllers\admin\AdminLoginController;
use App\Http\Controllers\admin\HomeController;
use App\Http\Controllers\admin\SettingController;
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');
});
Route::group(['prefix' => 'admin'], function () {
Route::group(['middleware' => 'admin.guest'], function () {
Route::get('/login', [AdminLoginController::class,'index'])->name('admin.login');
Route::post('/authenticate', [AdminLoginController::class,'authenticate'])->name('admin.authenticate');
});
Route::group(['middleware' => 'admin.auth'], function () {
Route::get('/home', [HomeController::class,'index'])->name('admin.dashboard');
Route::get('/logout', [HomeController::class,'logout'])->name('admin.logout');
Route::get('/chnage-password', [SettingController::class,'showchangepassword'])->name('admin.showchangepassword');
Route::post('/process-to-chnage-password', [SettingController::class,'processToChnagePassword'])->name('admin.processtochnagepassword');
});
});
create a migrations file in
D:\xampp\htdocs\laravel-admin\database\migrations\2024_03_04_073800_alter_users_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AlterUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->integer('roles')->default('1')->after('email');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('roles');
});
}
}
Create a D:\xampp\htdocs\laravel-admin\app\Http\Controllers\admin\AdminLoginController
<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
class AdminLoginController extends Controller
{
public function index(){
return view('admin.login');
}
public function authenticate(Request $request){
$validator = Validator::make($request->all(),[
'email' => 'required|email',
'password' => 'required'
]);
if($validator->passes()){
if(Auth::guard('admin')->attempt([
'email'=>$request->email,
'password'=>$request->password],
$request->get('remember')
)){
$admin = Auth::guard('admin')->user();
if($admin->roles == 2){
return redirect()->route('admin.dashboard');
}else{
$admin = Auth::guard('admin')->logout();
return redirect()->route('admin.login')
->with('error',"You are not authorized to access admin pannel");
}
}else{
return redirect()->route('admin.login')
->with('error',"Either Email/Password is incorrect");
}
}else{
return redirect()->route('admin.login')
->withErrors($validator)
->withInput($request->only('email'));
}
}
}
<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
class AdminLoginController extends Controller
{
public function index(){
return view('admin.login');
}
public function authenticate(Request $request){
$validator = Validator::make($request->all(),[
'email' => 'required|email',
'password' => 'required'
]);
if($validator->passes()){
if(Auth::guard('admin')->attempt([
'email'=>$request->email,
'password'=>$request->password],
$request->get('remember')
)){
$admin = Auth::guard('admin')->user();
if($admin->roles == 2){
return redirect()->route('admin.dashboard');
}else{
$admin = Auth::guard('admin')->logout();
return redirect()->route('admin.login')
->with('error',"You are not authorized to access admin pannel");
}
}else{
return redirect()->route('admin.login')
->with('error',"Either Email/Password is incorrect");
}
}else{
return redirect()->route('admin.login')
->withErrors($validator)
->withInput($request->only('email'));
}
}
}
HomeController
Create a D:\xampp\htdocs\laravel-admin\app\Http\Controllers\admin\HomeController
<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class HomeController extends Controller
{
public function index(){
return view('admin.dashboard');
// $admin = Auth::guard('admin')->user();
// echo "Welcome". $admin->name.' <a href="'.route('admin.logout').'">Logout</a>';
}
public function logout(){
Auth::guard('admin')->logout();
return redirect()->route('admin.login');
}
}
For Change Password : -
Create a D:\xampp\htdocs\laravel-admin\app\Http\Controllers\admin\SettingsController
<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
class SettingController extends Controller
{
public function showchangepassword(){
return view('admin.chnagepassword');
}
public function processToChnagePassword(Request $request){
$validator = validator::make($request->all(),[
'oldpassword'=>'required',
'newpassword'=>'required|min:5',
'cnfpassword'=>'required:same:newpassword',
]);
if($validator->passes()){
$id = Auth::guard('admin')->user()->id;
$admin = User::where('id',$id)->first();
if(!Hash::check($request->oldpassword,$admin->password)){
return redirect()->route('admin.showchangepassword')
->with('error',"Old Password is incorrect");
}else{
User::where('id',$id)->update([
'password' => Hash::make($request->newpassword)
]);
return redirect()->route('admin.login')
->with('success',"Password has been updated");
}
}else{
return redirect()->route('admin.showchangepassword')
->withErrors($validator);
}
}
}
D:\xampp\htdocs\laravel-admin\resources\views\admin\login.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel Shop :: Administrative Panel</title>
<!-- Google Font: Source Sans Pro -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
<!-- Font Awesome -->
<link rel="stylesheet" href="{{asset('admin-assets/plugins/fontawesome-free/css/all.min.css')}}">
<!-- Theme style -->
<link rel="stylesheet" href="{{asset('admin-assets/css/adminlte.min.css')}}">
<link rel="stylesheet" href="{{asset('admin-assets/css/custom.css')}}">
</head>
<body class="hold-transition login-page">
<div class="login-box">
<!-- /.login-logo -->
@include('admin.message')
<div class="card card-outline card-primary">
<div class="card-header text-center">
<a href="#" class="h3">Administrative Panel</a>
</div>
<div class="card-body">
<p class="login-box-msg">Sign in to start your session</p>
<form action="{{route('admin.authenticate')}}" method="post">
@csrf
<div class="input-group mb-3">
<input type="email" name="email" value="{{old('email')}}" class="form-control @error('email') is-invalid @enderror" placeholder="Email">
<div class="input-group-append">
<div class="input-group-text">
<span class="fas fa-envelope"></span>
</div>
</div>
</div>
@error('email')
<p>{{$message}}</p>
@enderror
<div class="input-group mb-3">
<input type="password" name="password" class="form-control @error('password') is-invalid @enderror" placeholder="Password">
<div class="input-group-append">
<div class="input-group-text">
<span class="fas fa-lock"></span>
</div>
</div>
</div>
@error('password')
<p>{{$message}}</p>
@enderror
<div class="row">
<!-- <div class="col-8">
<div class="icheck-primary">
<input type="checkbox" id="remember">
<label for="remember">
Remember Me
</label>
</div>
</div> -->
<!-- /.col -->
<div class="col-4">
<button type="submit" class="btn btn-primary btn-block">Login</button>
</div>
<!-- /.col -->
</div>
</form>
<p class="mb-1 mt-3">
<a href="forgot-password.html">I forgot my password</a>
</p>
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div>
<!-- ./wrapper -->
<!-- jQuery -->
<script src="{{asset('admin-assets/plugins/jquery/jquery.min.js')}}"></script>
<!-- Bootstrap 4 -->
<script src="{{asset('admin-assets/plugins/bootstrap/js/bootstrap.bundle.min.js')}}"></script>
<!-- AdminLTE App -->
<script src="{{asset('admin-assets/js/adminlte.min.js')}}"></script>
<!-- AdminLTE for demo purposes -->
<!-- <script src="js/demo.js"></script> -->
</body>
</html>
D:\xampp\htdocs\laravel-admin\resources\views\admin\dashboard.blade.php
@extends ('admin.layout.app')
@section('content')
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1>Dashboard</h1>
</div>
<div class="col-sm-6">
</div>
</div>
</div>
<!-- /.container-fluid -->
</section>
<!-- Main content -->
<section class="content">
<!-- Default box -->
<div class="container-fluid">
<div class="row">
<div class="col-lg-4 col-6">
<div class="small-box card">
<div class="inner">
<h3>150</h3>
<p>Total Orders</p>
</div>
<div class="icon">
<i class="ion ion-bag"></i>
</div>
<a href="#" class="small-box-footer text-dark">More info <i class="fas fa-arrow-circle-right"></i></a>
</div>
</div>
<div class="col-lg-4 col-6">
<div class="small-box card">
<div class="inner">
<h3>50</h3>
<p>Total Customers</p>
</div>
<div class="icon">
<i class="ion ion-stats-bars"></i>
</div>
<a href="#" class="small-box-footer text-dark">More info <i class="fas fa-arrow-circle-right"></i></a>
</div>
</div>
<div class="col-lg-4 col-6">
<div class="small-box card">
<div class="inner">
<h3>$1000</h3>
<p>Total Sale</p>
</div>
<div class="icon">
<i class="ion ion-person-add"></i>
</div>
<a href="javascript:void(0);" class="small-box-footer"> </a>
</div>
</div>
</div>
</div>
<!-- /.card -->
</section>
@endsection
@section('customjs')
<!-- <script src='deepak.js'></script> -->
@endsection
D:\xampp\htdocs\laravel-admin\resources\views\admin\chnagepassword.blade.php
@extends ('admin.layout.app')
@section('content')
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<div class="container-fluid my-2">
<div class="row mb-2">
<div class="col-sm-6">
<h1>Chnage Password</h1>
</div>
<div class="col-sm-6 text-right">
<a href="products.html" class="btn btn-primary">Back</a>
</div>
</div>
</div>
<!-- /.container-fluid -->
</section>
<!-- Main content -->
<section class="content">
<!-- Default box -->
<div class="container-fluid">
<div class="row">
<div class="col-md-8">
@include('admin.message')
<div class="card mb-3">
<div class="card-body">
<div class="row">
<form action="{{route('admin.processtochnagepassword')}}" method="post" >
@csrf
<div class="col-md-12">
<div class="mb-3">
<label for="title">Old Password</label>
<input type="password" name="oldpassword" id="title" class="form-control" placeholder="Old Password">
</div>
@error('oldpassword')
<p>{{$message}}</p>
@enderror
<div class="mb-3">
<label for="title">New Password</label>
<input type="password" name="newpassword" id="title" class="form-control" placeholder="Old Password">
</div>
@error('newpassword')
<p>{{$message}}</p>
@enderror
<div class="mb-3">
<label for="title">Confirm New Password</label>
<input type="password" name="cnfpassword" id="title" class="form-control" placeholder="Old Password">
</div>
@error('cnfpassword')
<p>{{$message}}</p>
@enderror
</div>
<div class="pb-5 pt-3">
<button class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- /.card -->
</section>
<!-- /.content -->
</div>
@endsection
@section('customjs')
<!-- <script src='deepak.js'></script> -->
@endsection
D:\xampp\htdocs\laravel-admin\resources\views\admin\message.blade.php
@if(Session::has('error'))
<div class="alert alert-danger alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h5><i class="icon fas fa-ban"></i> Error!</h5>
{{Session::get('error')}}
</div>
@endif
@if(Session::has('success'))
<div class="alert alert-success alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h5><i class="icon fas fa-check"></i> Success!</h5>
{{Session::get('success')}}
</div>
@endif
Some Importan Command :
php artisan --version
php artisan serve
php artisan make:model Product -m
php artisan migrate:fresh
php artisan make:controller ProductController
php artisan optimize
php artisan migrate
php artisan make:migration alter_users_table
php artisan make:provider RiakServiceProvider
================================
php artisan tinker
$model = new User();
$model->name = "admin";
$model->email = "admin@example.com";
$model->password = Hash::make('admin');
$model->roles = 2;
$model->save();
0 Comments