RESTful Resource Controllers of Laravel
Resource Controllers are built RESTful controllers around the resource. For example, if we have one resource called ‘Profile’. Now this RESTful controller handles HTTP request for ‘Profile’ resource.
using the make: controller command we can create a controller for profile
php artisan make:controller ProfileController
Now, this controller contains all the methods like GET, POST, PUT as per the requirement of function.
Resource full route for the controller
This single route declaration creates multiple routes to handle a variety of RESTful actions on the profile resource.
Types of Resource Routes: Partial, Naming, Nested….
- Partial Resource Routes: When declaring a resource route, you may specify a subset of actions to handle on the route:
['only' => ['index', 'show']]);
['except' => ['create', 'store', 'update', 'destroy']]);
2. Naming Resource Routes:
['names' => ['create' => 'profile.build']]);
3. Nested Resource Routes:
Sometimes you may need to define routes to a “nested” resource. For example, a profile resource may have multiple “posts” that attached to the photo and maybe caption or description. To “nest” resource controllers, use “dot” notation in your route declaration: