Resource Controllers & RESTful Conventions
Resource controllers implement all 7 RESTful CRUD operations with a single artisan command. Route::resource() generates index (list), create (show form), store (save), show (view one), edit (edit form), update (save changes), and destroy (delete) routes with proper HTTP methods and named routes — all in one line. This convention is the industry standard for building predictable, maintainable APIs and web applications.
Resource Routes
// php artisan make:controller PostController --resource
Route::resource('posts', PostController::class);
// Creates 7 routes automatically:
// GET /posts → index → posts.index
// GET /posts/create → create → posts.create
// POST /posts → store → posts.store
// GET /posts/{post} → show → posts.show
// GET /posts/{post}/edit → edit → posts.edit
// PUT /posts/{post} → update → posts.update
// DELETE /posts/{post} → destroy → posts.destroy
// API Resource (no HTML form routes — create/edit excluded)
Route::apiResource('posts', PostController::class);
// 5 routes: index, store, show, update, destroy
// Limit which methods to generate
Route::resource('photos', PhotoController::class)
->only(['index', 'show']);
Route::resource('comments', CommentController::class)
->except(['create', 'edit']);
// Nested resources
Route::resource('posts.comments', PostCommentController::class);
// GET /posts/{post}/comments → posts.comments.index
// POST /posts/{post}/comments → posts.comments.store
// GET /posts/{post}/comments/{comment} → posts.comments.show
// Shallow nesting (preferred for cleaner URLs)
Route::resource('posts.comments', PostCommentController::class)->shallow();
// Index/store are nested; show/update/destroy are shallowBenefits of Resource Controllers
- Convention over configuration: every developer immediately knows what index lists, store creates, show displays, destroy deletes — no guessing required
- RESTful URLs are predictable across your entire application and make the API self-documenting to API consumers
- Route::resource() generates 7 routes in one line with consistent naming — posts.index, posts.show, etc. — reducing the chance of naming inconsistencies
- API resource controllers exclude create/edit (no HTML forms needed for APIs) — use apiResource() for JSON APIs
- Route::resources() registers multiple resource controllers at once: Route::resources(['photos' => PhotoController::class, 'posts' => PostController::class])
Tip
Tip
Practice Resource Controllers RESTful Conventions in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Middleware = filters that run before/after your controller logic
Practice Task
Note
Practice Task — (1) Write a working example of Resource Controllers RESTful Conventions from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Resource Controllers RESTful Conventions is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready laravel code.
Key Takeaways
- Resource controllers implement all 7 RESTful CRUD operations with a single artisan command.
- Convention over configuration: every developer immediately knows what index lists, store creates, show displays, destroy deletes — no guessing required
- RESTful URLs are predictable across your entire application and make the API self-documenting to API consumers
- Route::resource() generates 7 routes in one line with consistent naming — posts.index, posts.show, etc. — reducing the chance of naming inconsistencies