Overview
Controllers in Dashboard Laravel handle incoming HTTP requests, process business logic, and return responses. They act as the intermediary between routes and models, following the MVC pattern.All controllers are located in
app/Http/Controllers/ and extend the base Controller class.Controller Architecture
The application follows Laravel 11’s controller structure:Base Controller
Abstract foundation for all controllers
AuthController
Handles login, registration, and logout
HomeController
Dashboard home page logic
Base Controller
Location:app/Http/Controllers/Controller.php
Base Controller Details
Base Controller Details
Purpose: Provides common functionality for all application controllers.Key Features:
- Abstract class that cannot be instantiated directly
- Extends Laravel’s base controller functionality
- Serves as foundation for dependency injection
- Can include shared helper methods
AuthController
Location:app/Http/Controllers/AuthController.php
Handles all authentication-related operations including login, registration, and logout.
AuthController Methods
showLogin() - Display Login Page
showLogin() - Display Login Page
Route: Logic:
GET /Purpose: Display the login form or redirect authenticated users to dashboard.- Check if user is already authenticated using
Auth::check() - If authenticated, redirect to
/dashboard - If not authenticated, display
home.blade.phpview (login page)
login(Request) - Process Login
login(Request) - Process Login
Route: Validation Rules:
POST /loginPurpose: Validate credentials and authenticate user.email- Required, must be valid email formatpassword- Required, minimum 6 characters
- Validate incoming request data
- Attempt authentication with
Auth::attempt() - Check “remember me” checkbox status
- On success: Regenerate session and redirect to dashboard
- On failure: Redirect back with error message and preserve input
- Session regeneration prevents session fixation attacks
- Custom Spanish error messages for better UX
- Remember me functionality for persistent sessions
showRegister() - Display Registration Page
showRegister() - Display Registration Page
Route: Logic:
GET /signupPurpose: Display the registration form or redirect authenticated users.- Check authentication status
- Redirect authenticated users to dashboard
- Show
signup.blade.phpfor guests
register(Request) - Process Registration
register(Request) - Process Registration
Route: Validation Rules:
POST /signupPurpose: Create new user account and auto-login.name- Required, string, max 255 charactersemail- Required, valid email, unique in users tablepassword- Required, min 6 characters, must match confirmation
- Validate registration data
- Create new User record with hashed password
- Automatically login the new user
- Redirect to dashboard
- Password hashing with
Hash::make() - Email uniqueness check prevents duplicates
- Password confirmation validation
- Custom Spanish error messages
logout(Request) - Process Logout
logout(Request) - Process Logout
Route: Logic Flow:
POST /logoutPurpose: End user session and clear authentication.- Logout user with
Auth::logout() - Invalidate current session
- Regenerate CSRF token for security
- Redirect to home page
- Complete session invalidation
- CSRF token regeneration prevents token reuse
- Clean logout prevents session hijacking
HomeController
Location:app/Http/Controllers/HomeController.php
HomeController Details
HomeController Details
Purpose: Placeholder for home page logic.Currently empty - logic is handled directly in routes using closures:Future Use Cases:
- Dashboard data aggregation
- Statistics calculation
- Recent activity fetching
- User preferences loading
Request Handling Flow
Understanding how requests flow through controllers:Request Validation
Controllers use Laravel’s validation system for data integrity:Validation Syntax
Common Validation Rules Used
required
Field must be present and not empty
Must be valid email format
min:6
Minimum length of 6 characters
unique:table,column
Value must be unique in database
confirmed
Must match field_confirmation input
string
Must be a string data type
max:255
Maximum length of 255 characters
Custom Error Messages
The application uses Spanish error messages for better user experience:Response Handling
Controllers can return various response types:View Responses
View Responses
Return a rendered Blade view:Views are located in
resources/views/.Redirect Responses
Redirect Responses
Redirect to another URL:
Redirect with Data
Redirect with Data
Pass data to the redirected page:
Dependency Injection
Controllers use dependency injection for accessing services:Injected Dependencies
- Request - Access HTTP request data
- Auth - Authentication services
- Hash - Password hashing
- User Model - Database operations
Route-Controller Mapping
Fromroutes/web.php:
Named routes allow easy URL generation in views using
route('name')Closure-Based Routes
Some routes use closures instead of controllers:- Simple view rendering without logic
- Quick prototyping
- Static pages
- Complex business logic
- Database operations
- Form processing
- Multiple related actions
Best Practices
Single Responsibility
Each controller method should handle one specific action
Validation
Always validate user input before processing
Session Security
Regenerate sessions after authentication state changes
Error Handling
Provide clear, user-friendly error messages
Type Hinting
Use type hints for better IDE support and error prevention
Named Routes
Use named routes for maintainable URL generation
Next Steps
Views
Learn about Blade templating
Models
Explore Eloquent models
Routing
Understand route configuration
