0
0
Laravelframework~10 mins

Resource routes in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Resource routes
Define Resource Route
Laravel Maps URLs
Create Controller Methods
Handle HTTP Requests
Return Response
User Sees Result
Resource routes connect URLs to controller methods automatically, handling common actions like list, create, update, and delete.
Execution Sample
Laravel
Route::resource('posts', PostController::class);
This single line creates multiple routes for posts, linking URLs to PostController methods.
Execution Table
StepURLHTTP MethodController MethodAction Description
1/postsGETindexShow list of posts
2/posts/createGETcreateShow form to create a post
3/postsPOSTstoreSave new post
4/posts/{post}GETshowShow a single post
5/posts/{post}/editGETeditShow form to edit post
6/posts/{post}PUT/PATCHupdateUpdate existing post
7/posts/{post}DELETEdestroyDelete a post
8---No more routes, resource routes complete
💡 All 7 resource routes are registered, covering standard CRUD actions.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
Registered Routes[][GET /posts][GET /posts, GET /posts/create][GET /posts, GET /posts/create, POST /posts][GET /posts, GET /posts/create, POST /posts, GET /posts/{post}][GET /posts, GET /posts/create, POST /posts, GET /posts/{post}, GET /posts/{post}/edit][GET /posts, GET /posts/create, POST /posts, GET /posts/{post}, GET /posts/{post}/edit, PUT/PATCH /posts/{post}][GET /posts, GET /posts/create, POST /posts, GET /posts/{post}, GET /posts/{post}/edit, PUT/PATCH /posts/{post}, DELETE /posts/{post}]
Key Moments - 3 Insights
Why does one Route::resource line create many URLs?
Because Laravel automatically maps standard CRUD URLs to controller methods as shown in the execution_table steps 1 to 7.
What HTTP method is used to update a post?
PUT or PATCH is used to update, as shown in step 6 of the execution_table.
How does Laravel know which controller method to call for /posts/5/edit?
It matches the URL pattern /posts/{post}/edit and calls the 'edit' method, as shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which controller method handles the URL '/posts' with GET?
Aindex
Bshow
Cstore
Dedit
💡 Hint
Check step 1 in the execution_table for URL '/posts' with GET method.
At which step does Laravel register the route to delete a post?
AStep 4
BStep 3
CStep 7
DStep 6
💡 Hint
Look for the DELETE HTTP method in the execution_table.
If you add Route::resource('comments', CommentController::class), how does the variable 'Registered Routes' change after step 8?
AIt replaces all post routes with comment routes
BIt adds 7 new routes for comments
CIt adds only one new route for comments
DIt does not change
💡 Hint
Resource routes always add 7 routes per resource as shown in variable_tracker.
Concept Snapshot
Resource routes in Laravel map standard URLs to controller methods automatically.
Use Route::resource('name', Controller::class) to create 7 routes.
These routes cover index, create, store, show, edit, update, destroy.
Each route uses specific HTTP methods and URL patterns.
This saves time and keeps routing organized.
Full Transcript
Resource routes in Laravel let you create many routes with one line. When you write Route::resource('posts', PostController::class), Laravel creates 7 routes for you. These routes handle showing all posts, showing a form to create a post, saving a new post, showing one post, editing a post, updating a post, and deleting a post. Each route uses a specific URL and HTTP method. For example, GET /posts calls the index method to list posts. DELETE /posts/{post} calls the destroy method to delete a post. This automatic mapping helps you write less code and keep your app organized.