📖 Scenario: You are building a simple blog application where users can manage posts. You want to set up routes that handle all the common actions like listing posts, showing a single post, creating, updating, and deleting posts.
🎯 Goal: Set up a resource route in Laravel that automatically creates all the standard routes for managing posts.
📋 What You'll Learn
Create a route group for posts using Laravel's resource routing
Use the exact resource name posts
Use the controller named PostController
Ensure the resource route is registered in the routes/web.php file
💡 Why This Matters
🌍 Real World
Resource routes in Laravel help quickly set up all the common routes needed for CRUD operations on resources like posts, users, or products.
💼 Career
Understanding resource routes is essential for Laravel developers to build RESTful web applications efficiently and follow best practices.
Progress0 / 4 steps
1
Create the posts resource route
In the routes/web.php file, add a resource route for posts using the PostController. Write the exact line: Route::resource('posts', PostController::class);
Laravel
Hint
Use Laravel's Route::resource method with the exact resource name 'posts' and controller PostController::class.
2
Add a route name prefix
Modify the resource route to add a name prefix blog. so that all route names start with blog.. Write the exact code: Route::resource('posts', PostController::class)->names('blog');
Laravel
Hint
Use the ->names('blog') method chained to the resource route to add the prefix.
3
Limit resource routes to only index and show
Change the resource route to only include the index and show methods. Write the exact code: Route::resource('posts', PostController::class)->only(['index', 'show']);
Laravel
Hint
Use the ->only(['index', 'show']) method chained to the resource route to limit the routes.
4
Exclude the create and edit routes
Modify the resource route to exclude the create and edit methods. Write the exact code: Route::resource('posts', PostController::class)->except(['create', 'edit']);
Laravel
Hint
Use the ->except(['create', 'edit']) method chained to the resource route to exclude those routes.
Practice
(1/5)
1. What does the Route::resource('photos', PhotoController::class) method do in Laravel?
easy
A. Creates multiple routes for common CRUD actions automatically.
B. Creates a single route for the index method only.
C. Creates routes only for the create and edit methods.
D. Deletes all existing routes related to photos.
Solution
Step 1: Understand the purpose of Route::resource
This method generates routes for all standard CRUD actions (index, create, store, show, edit, update, destroy) automatically.
Step 2: Recognize the effect on routes
It saves time by creating multiple routes with correct HTTP verbs and URLs for the resource controller.
Final Answer:
Creates multiple routes for common CRUD actions automatically. -> Option A
Quick Check:
Resource routes = multiple CRUD routes [OK]
Hint: Resource routes generate all CRUD URLs in one line [OK]
Common Mistakes:
Thinking it creates only one route
Confusing resource with single route methods
Assuming it deletes routes
2. Which of the following is the correct syntax to create resource routes for a controller named PostController?
easy
A. Route::resource('posts', 'PostController::class');
B. Route::resource('posts', 'PostController');
C. Route::resource('posts', PostController);
D. Route::resource('posts', PostController::class);
A. The except() method requires an array, not a string.
B. UserController::class should be a string.
C. Resource routes cannot use except() method.
D. The route name 'users' is invalid.
Solution
Step 1: Check except() method usage
The except() method expects an array of action names to exclude, not a single string.
Step 2: Identify correct syntax
It should be except(['store']) with square brackets to pass an array.
Final Answer:
The except() method requires an array, not a string. -> Option A
Quick Check:
Use except(['action']) with array brackets [OK]
Hint: Pass array to except(), not a single string [OK]
Common Mistakes:
Passing string instead of array to except()
Using quotes incorrectly around controller class
Thinking except() is not allowed
5. You want to create resource routes for ArticleController but only allow routes for creating, storing, and editing articles. Which is the correct route definition?
hard
A. Route::resource('articles', ArticleController::class)->only(['create', 'store', 'edit']);
B. Route::resource('articles', ArticleController::class)->only(['create', 'edit']);
C. Route::resource('articles', ArticleController::class)->except(['index', 'show', 'update', 'destroy']);
D. Route::resource('articles', ArticleController::class)->except(['store']);
Solution
Step 1: Identify required actions
We want routes for create, store, and edit only.
Step 2: Use except() to exclude unwanted actions
Excluding index, show, update, and destroy leaves only create, store, and edit routes.
Step 3: Confirm only() option
Route::resource('articles', ArticleController::class)->only(['create', 'store', 'edit']); correctly includes the store route, but using except() as in Route::resource('articles', ArticleController::class)->except(['index', 'show', 'update', 'destroy']); is also correct and often preferred for excluding unwanted routes.
Final Answer:
Route::resource('articles', ArticleController::class)->except(['index', 'show', 'update', 'destroy']); -> Option C