In this tutorial you will learn about the Laravel Custom Logout Example and its application with practical example.
In this article, we will learn to create laravel custom logout in your laravel project. It is easy to create laravel custom logout.
Create Laravel Custom Logout
Follow step by step laravel custom logout tutorial to create custom logout in laravel.
Create Route
Let’s open web.php file add the following route to it:
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?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'); |
Create a UserController Controller
Now, create a User Controller Controller name UserController. Use the below command to create controller.
1 |
php artisan make:controller <b>UserController</b> |
Once the above command executed, it will create a resource controller file “UserController.php” in “app/Http/Controllers/” directory. Go to “UserController.php” and put the following code in it.
app/Http/Controllers/UserController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<?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'); } } |
Create a User Blade File
In this step, we will create a view file named user.blade.php inside “resources/views/” directory and put the following code inside it –
resources/views/user.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<!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> |
Now we have created custom user logout functionality, restart the development server using following artisan command –
1 |
php artisan serve |
and visit the following URL in browser to see custom logout feature:
1 |
http://localhost:8000/users/index |