Laravel Blade Templating: Master the Basics of Laravel's View Engine
Create dynamic, reusable views with Laravel's powerful Blade templating engine.
Routing is the mechanism that maps URLs to your application's logic. Think of routes as "directions" that tell Laravel what to do when someone visits a specific URL in your application.
In simple terms: Routes define the endpoints of your application and what happens when users visit them.
All web routes are defined in routes/web.php file. Let's start with the most basic route.
// routes/web.php
Route::get('/', function () {
return 'Welcome to TeachyLeaf Blog!';
});
When you visit http://your-app.com/, you'll see "Welcome to TeachyLeaf Blog!"
Laravel supports all HTTP methods. Here are the most common ones:
Route::get('/about', function () {
return 'About Us Page';
});
Route::get('/contact', function () {
return view('contact');
});
Route::post('/contact', function () {
// Process form submission
return 'Form submitted successfully!';
});
Route::put('/users/{id}', function ($id) {
// Update user
return "User {$id} updated";
});
Route::patch('/profile/{id}', function ($id) {
// Partial update
return "Profile {$id} updated";
});
Route::delete('/users/{id}', function ($id) {
// Delete user
return "User {$id} deleted";
});
// Single parameter
Route::get('/users/{id}', function ($id) {
return "User ID: {$id}";
});
// Multiple parameters
Route::get('/posts/{category}/{slug}', function ($category, $slug) {
return "Category: {$category}, Slug: {$slug}";
});
Route::get('/users/{id?}', function ($id = null) {
return $id ? "User ID: {$id}" : 'All Users';
});
Route::get('/users/{id}', function ($id) {
return "User ID: {$id}";
})->where('id', '[0-9]+'); // Only numeric IDs
Route::get('/posts/{slug}', function ($slug) {
return "Post: {$slug}";
})->where('slug', '[A-Za-z-]+'); // Only letters and hyphens
Named routes make it easy to generate URLs throughout your application.
Route::get('/home', function () {
return view('home');
})->name('homepage');
Route::get('/users/profile', function () {
return view('profile');
})->name('profile');
// In controllers
return redirect()->route('homepage');
// In Blade templates
<a href="{{ route('homepage') }}">Home</a>
<a href="{{ route('profile') }}">My Profile</a>
// With parameters
Route::get('/users/{id}', function ($id) {
// ...
})->name('users.show');
// Generate URL with parameter
route('users.show', ['id' => 1]); // Returns /users/1
Route groups allow you to apply attributes to multiple routes.
// Admin routes with /admin prefix
Route::prefix('admin')->group(function () {
Route::get('/users', function () {
return 'Admin Users Page';
});
Route::get('/settings', function () {
return 'Admin Settings';
});
});
// URLs: /admin/users, /admin/settings
// Apply auth middleware to multiple routes
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', function () {
return 'User Dashboard';
});
Route::get('/profile', function () {
return 'User Profile';
});
});
Route::name('admin.')->group(function () {
Route::get('/users', function () {
// ...
})->name('users'); // Named route: admin.users
Route::get('/settings', function () {
// ...
})->name('settings'); // Named route: admin.settings
});
Instead of putting logic in route files, connect routes to controller methods.
// Single method
Route::get('/users', [UserController::class, 'index']);
// Multiple methods
Route::get('/users/{id}', [UserController::class, 'show']);
Route::post('/users', [UserController::class, 'store']);
Route::put('/users/{id}', [UserController::class, 'update']);
Route::delete('/users/{id}', [UserController::class, 'destroy']);
Resource routes create multiple routes for a controller with a single line.
Route::resource('posts', PostController::class);
This single line creates 7 routes automatically:
| HTTP Method | URL | Controller Method | Route Name |
|---|---|---|---|
| GET | /posts | index | posts.index |
| GET | /posts/create | create | posts.create |
| POST | /posts | store | posts.store |
| GET | /posts/{id} | show | posts.show |
| GET | /posts/{id}/edit | edit | posts.edit |
| PUT/PATCH | /posts/{id} | update | posts.update |
| DELETE | /posts/{id} | destroy | posts.destroy |
Route::apiResource('posts', PostController::class);
Creates only: index, store, show, update, destroy (perfect for APIs).
Route::resources([
'posts' => PostController::class,
'users' => UserController::class,
'comments' => CommentController::class
]);
// Only these methods
Route::resource('posts', PostController::class)->only([
'index', 'show', 'store'
]);
// Exclude these methods
Route::resource('posts', PostController::class)->except([
'create', 'edit', 'update', 'destroy'
]);
Here's a complete example for a blog:
// routes/web.php
// Public routes
Route::get('/', [PostController::class, 'index'])->name('home');
Route::get('/about', function () {
return view('about');
})->name('about');
// Post routes
Route::get('/posts/{slug}', [PostController::class, 'show'])->name('posts.show');
Route::get('/categories/{category}', [PostController::class, 'byCategory'])->name('posts.category');
// Authentication routes (provided by Laravel)
Auth::routes();
// Protected admin routes
Route::middleware(['auth'])->prefix('admin')->name('admin.')->group(function () {
Route::get('/dashboard', [AdminController::class, 'dashboard'])->name('dashboard');
// Resource routes for CRUD operations
Route::resource('posts', AdminPostController::class);
Route::resource('categories', CategoryController::class);
});
// API routes (in routes/api.php)
Route::prefix('api')->group(function () {
Route::apiResource('posts', ApiPostController::class);
Route::get('search', [ApiPostController::class, 'search']);
});
Laravel can automatically inject model instances based on route parameters.
// Laravel automatically finds the Post model
Route::get('/posts/{post}', function (Post $post) {
return view('posts.show', compact('post'));
});
// With custom column
Route::get('/posts/{post:slug}', function (Post $post) {
return view('posts.show', compact('post'));
});
// In RouteServiceProvider.php
public function boot()
{
Route::model('user', User::class);
// Or with custom logic
Route::bind('post', function ($value) {
return Post::where('slug', $value)->firstOrFail();
});
}
1. What is Laravel routing?
Laravel routing maps URLs to controller actions or closures, defining how your application responds to HTTP requests.
2. What's the difference between GET and POST routes?
GET requests are for retrieving data and should be idempotent. POST requests are for creating or modifying data and can have side effects.
3. What are resource controllers?
Resource controllers provide built-in methods for CRUD operations (index, create, store, show, edit, update, destroy) that can be mapped with a single route declaration.
4. How do you create a named route?
Use the name() method: Route::get('/about', fn() => 'About')->name('about');
5. What is route model binding?
Route model binding automatically injects model instances into your routes based on the route parameters, eliminating the need to manually fetch models.
6. How do you group routes?
Use Route::group() with attributes like prefix, middleware, or name prefix to apply common settings to multiple routes.
php artisan route:cache
// Basic routes
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::any($uri, $callback); // Any method
// Resource routes
Route::resource($uri, $controller);
Route::apiResource($uri, $controller);
// Route parameters
Route::get('user/{id}', fn($id) => $id);
Route::get('user/{id?}', fn($id = 1) => $id);
// Named routes
Route::get('about', fn() => 'About')->name('about');
// Groups
Route::prefix('admin')->group(fn() => {});
Route::middleware('auth')->group(fn() => {});
Now you're ready to handle any routing scenario in Laravel! In our next post, we'll dive into Laravel Controllers: Creating and Using Them to Handle Logic where you'll learn to organize your application logic properly.