Resource routes help you quickly create all the common routes for a controller in one line. This saves time and keeps your code clean.
Resource routes in Laravel
Route::resource('resource-name', ControllerName::class);
This single line creates multiple routes for standard actions.
The 'resource-name' is the URL segment, and the controller handles the logic.
Route::resource('posts', PostController::class);
Route::resource('users', UserController::class);
Route::resource('products', ProductController::class)->only(['index', 'show']);
Route::resource('orders', OrderController::class)->except(['destroy']);
This code creates all standard routes for managing books. Laravel will map URLs like /books, /books/create, /books/{book}, etc., to the correct methods in BookController.
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\BookController; Route::resource('books', BookController::class); // BookController should have methods: index, create, store, show, edit, update, destroy
Resource routes follow RESTful URL and HTTP method conventions automatically.
You can customize which routes to create using only() or except() methods.
Make sure your controller has the matching methods for the routes to work properly.
Resource routes create many routes in one line for common actions.
They follow RESTful patterns for URLs and HTTP methods.
Use only() or except() to control which routes are generated.
Practice
Route::resource('photos', PhotoController::class) method do in Laravel?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 AQuick Check:
Resource routes = multiple CRUD routes [OK]
- Thinking it creates only one route
- Confusing resource with single route methods
- Assuming it deletes routes
PostController?Solution
Step 1: Identify correct controller reference syntax
In Laravel, the controller class should be passed using the ::class constant without quotes.Step 2: Check each option
Route::resource('posts', PostController::class); correctly uses PostController::class without quotes, which is the modern and recommended syntax.Final Answer:
Route::resource('posts', PostController::class); -> Option DQuick Check:
Use ControllerName::class without quotes [OK]
- Passing controller name as a string with quotes
- Omitting ::class
- Using quotes around ::class
Route::resource('books', BookController::class)->only(['index', 'show']);, which URLs will be available?Solution
Step 1: Understand the only() method
The only() method limits resource routes to specified actions. Here, only 'index' and 'show' routes are created.Step 2: Identify routes for index and show
Index route is GET /books, show route is GET /books/{book}.Final Answer:
/books (GET) and /books/{book} (GET) -> Option BQuick Check:
only(['index','show']) = GET /books and GET /books/{id} [OK]
- Confusing HTTP verbs for actions
- Assuming create/edit routes are included
- Mixing POST or DELETE with index/show
Route::resource('users', UserController::class)->except('store');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 AQuick Check:
Use except(['action']) with array brackets [OK]
- Passing string instead of array to except()
- Using quotes incorrectly around controller class
- Thinking except() is not allowed
ArticleController but only allow routes for creating, storing, and editing articles. Which is the correct route definition?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 CQuick Check:
except(['index','show','update','destroy']) = create, store, edit routes [OK]
- Using only() but missing store route
- Excluding wrong actions
- Confusing create and store routes
Ready to prove it?
Pass the test and this pattern moves unknown
