10-Apr-2023
.
Admin
Hi guys, Today i will explained to the laravel custom logout in your laravel project.laravel custom logout is so easy to use.so you can just follow my step by step and learn laravel custom logout.
So let's start to the example and follow to the my all step.
Solution
Auth::logout();
Example :
Step 1: Create Route
Last step to create a route in web.php file and use this code.
Route :routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
/*
|--------------------------------------------------------------------------
| 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('users/index', [UserController::class, 'index'])->name('users.index');
Route::post('logout', [UserController::class, 'logout'])->name('logout');
Step 2: Create a UserController Controller
Next you can require to the User Controller so create a User Controller in just following command through.
Controller : app/Http/Controllers/UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use Validator;
use Auth;
class UserController extends Controller
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
return view('users');
}
public function logout(Request $request) {
Auth::logout();
return redirect('/login');
}
}
Step 4: Create a User Blade File
Next you can require to the user.blade.php so create a user.blade.php in just following step.
View :resources/views/user.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel Custom Logout</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h2>Laravel Custom Logout</h2>
<div class="card">
<div class="card-header">Laravel Custom Logout</div>
<div class="card-body"><div class="col-md-12 text-center">
<form method="POST" action="{{ route('logout') }}">
@csrf
<button type="button" class="btn btn-primary">Logout</button>
</form>
</div></div>
</div>
</div>
</body>
</html>
So, finally we are done with our code we can get below output.
php artisan serve
Browser url run : http://localhost:8000/users/index
#Laravel 8
#Laravel 7