A resource controller maps the seven CRUD actions to one class. --model type-hints Product in each stub for route model binding (Route Model Binding); --requests would add the form requests of Validation.
php artisan make:controller ProductController --resource --model=Product
php artisan make:controller Api/ProductController --api --model=ProductRoute::resource('products', ProductController::class) registers the routes below. Route::apiResource() omits create and edit, which only serve HTML forms; the bookshop adds ->names('api.products') so its API names cannot collide with the web ones.
| Verb | URI | Action | Route name |
|---|---|---|---|
| GET | /products | index | products.index |
| GET | /products/create | create | products.create |
| POST | /products | store | products.store |
| GET | /products/{product} | show | products.show |
| GET | /products/{product}/edit | edit | products.edit |
| PUT/PATCH | /products/{product} | update | products.update |
| DELETE | /products/{product} | destroy | products.destroy |
only() and except() trim the set, as the admin OrderController of Prefixes and Subdomains did. Register extra routes such as /products/popular before resource(), or {product} captures popular. ->missing(fn () => ...) replaces the 404 for an unknown model.