Good question 👍
I’ll show you step by step logout system in Laravel 10 using 4-Layer Architecture with proper structure.
We will follow this structure:
Controller → Service → Repository → Model
✅ Step 1: Install Laravel Auth (if not installed)
If you don’t have authentication installed:
composer require laravel/breeze --dev
php artisan breeze:install
npm install
npm run build
php artisan migrate
Now login/register system is ready.
✅ Step 2: Create 4-Layer Structure
If you don’t already have layers, create them.
Create folders:
app/
├── Http/Controllers
├── Services
├── Repositories
│ ├── Contracts
│ └── Eloquent
✅ Step 3: Create Repository Interface
📁 app/Repositories/Contracts/AuthRepositoryInterface.php
<?php
namespace App\Repositories\Contracts;
interface AuthRepositoryInterface
{
public function logout();
}
✅ Step 4: Create Repository Implementation
📁 app/Repositories/Eloquent/AuthRepository.php
<?php
namespace App\Repositories\Eloquent;
use App\Repositories\Contracts\AuthRepositoryInterface;
use Illuminate\Support\Facades\Auth;
class AuthRepository implements AuthRepositoryInterface
{
public function logout()
{
Auth::logout();
}
}
✅ Step 5: Bind Repository in AppServiceProvider
Open:
📁 app/Providers/AppServiceProvider.php
Add inside register():
use App\Repositories\Contracts\AuthRepositoryInterface;
use App\Repositories\Eloquent\AuthRepository;
public function register(): void
{
$this->app->bind(AuthRepositoryInterface::class, AuthRepository::class);
}
✅ Step 6: Create Auth Service
📁 app/Services/AuthService.php
<?php
namespace App\Services;
use App\Repositories\Contracts\AuthRepositoryInterface;
class AuthService
{
protected $authRepository;
public function __construct(AuthRepositoryInterface $authRepository)
{
$this->authRepository = $authRepository;
}
public function logout()
{
return $this->authRepository->logout();
}
}
✅ Step 7: Create Controller
php artisan make:controller AuthController
📁 app/Http/Controllers/AuthController.php
<?php
namespace App\Http\Controllers;
use App\Services\AuthService;
use Illuminate\Http\Request;
class AuthController extends Controller
{
protected $authService;
public function __construct(AuthService $authService)
{
$this->authService = $authService;
}
public function logout(Request $request)
{
$this->authService->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect('/login');
}
}
✅ Step 8: Add Route
📁 routes/web.php
use App\Http\Controllers\AuthController;
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
✅ Step 9: Add Logout Button in Blade
<form method="POST" action="{{ route('logout') }}">
@csrf
<button type="submit">Logout</button>
</form>
⚠️ Logout must be POST request (not GET).
🔥 Flow Explanation
When user clicks logout:
Blade Form
↓
Route
↓
AuthController
↓
AuthService
↓
AuthRepository
↓
Auth::logout()
Then session invalidated → user redirected to login.
🎯 Important (Security)
This is important:
$request->session()->invalidate();
$request->session()->regenerateToken();
It protects from session fixation attack.
🎉 All Commands Used
composer require laravel/breeze --dev
php artisan breeze:install
npm install
npm run build
php artisan migrate
php artisan make:controller AuthController
If you want, I can also show:
✅ Login in 4-layer architecture
✅ Register in 4-layer
✅ API logout (Sanctum)
✅ Livewire logout
✅ Complete Auth 4-layer project structure
Just tell me 👍
0 Comments