Routing and controllers are core components of Laravel applications.
Routes define application URLs, while controllers handle request processing and business logic.
What is Routing?
Routing is the process of defining how an application responds to specific URLs and HTTP requests.
Laravel routes map URLs to functions or controller methods.
Route Files in Laravel
Laravel stores routes inside the routes directory.
The most commonly used file is web.php for web application routes.
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return 'Welcome to Laravel';
});
?>HTTP Request Methods
Laravel supports different HTTP request methods such as GET, POST, PUT, PATCH, and DELETE.
Each method is used for specific types of operations.
GET Routes
GET routes are commonly used to display pages or retrieve information.
<?php
Route::get('/about', function () {
return view('about');
});
?>POST Routes
POST routes are used to submit data to the server, such as form submissions.
<?php
Route::post('/submit', function () {
return 'Form Submitted';
});
?>Route Parameters
Route parameters allow dynamic values to be passed through URLs.
<?php
Route::get('/user/{id}', function ($id) {
return 'User ID: ' . $id;
});
?>Named Routes
Named routes provide convenient references for generating URLs and redirects.
<?php
Route::get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
?>What are Controllers?
Controllers organize application logic into reusable classes.
Instead of placing logic directly inside routes, Laravel encourages developers to use controllers.
Creating a Controller
Laravel provides Artisan commands for generating controllers quickly.
php artisan make:controller UserControllerBasic Controller Example
<?php
namespace App\Http\Controllers;
class UserController extends Controller
{
public function index() {
return 'Users Page';
}
}
?>Connecting Routes with Controllers
Routes can call specific controller methods for handling requests.
<?php
use App\Http\Controllers\UserController;
Route::get('/users', [UserController::class, 'index']);
?>Returning Views from Controllers
Controllers commonly return Blade views along with application data.
<?php
public function dashboard() {
return view('dashboard');
}
?>Passing Data to Views
Controllers can send data to views using arrays or compact() functions.
<?php
public function profile() {
$name = 'Rahul';
return view('profile', compact('name'));
}
?>Resource Controllers
Resource controllers provide built-in methods for CRUD operations.
<?php
Route::resource('posts', PostController::class);
?>Route Groups
Route groups allow developers to organize routes with shared middleware, prefixes, or namespaces.
<?php
Route::prefix('admin')->group(function () {
Route::get('/dashboard', function () {
return 'Admin Dashboard';
});
});
?>Middleware in Routes
Middleware filters requests before they reach controllers.
It is commonly used for authentication and access control.
<?php
Route::get('/profile', function () {
return view('profile');
})->middleware('auth');
?>Benefits of Controllers
Controllers keep routes clean and improve code organization in large applications.
They also encourage reusable and maintainable application logic.
Common Beginner Mistakes
Placing too much logic inside route closures is a common beginner mistake.
Developers should move business logic into controllers for cleaner architecture.
Best Practices
Use controllers to organize logic, name important routes, and apply middleware for security.
Maintain a clean route structure to improve readability and scalability.
Real-World Importance
Routing and controllers are used in every Laravel application to manage user requests and application flow.
Understanding these concepts is essential for professional Laravel development.
Summary
Laravel routing defines how applications respond to URLs, while controllers manage request handling and application logic.
Mastering routing and controllers helps developers build organized, scalable, and maintainable Laravel applications.