Resource Controllers

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.

Generating the web and API product controllersShell
php artisan make:controller ProductController --resource --model=Product
php artisan make:controller Api/ProductController --api --model=Product

Route::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.

The seven routes registered by Route::resource('products', ...)
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.