An Interest In:
Web News this Week
- September 18, 2023
- September 17, 2023
- September 16, 2023
- September 15, 2023
- September 14, 2023
- September 13, 2023
- September 12, 2023
Laravel routes: apiResource vs resource
Laravel routes application: using apiResource and resource. These methods help you create routes for a resource controller, following the RESTful conventions.
apiResource Function:
Route::apiResource('users', 'PostController');GET /users index users.indexPOST /users store users.storeGET /users/{user} show users.showPUT|PATCH /users/{user} update users.updateDELETE /users/{user} destroy users.destroy
resource Function
Route::resource('users', 'PostController');GET /users index users.indexGET /users/create create users.createPOST /users store users.storeGET /users/{user} show users.showGET /users/{user}/edit edit users.editPUT|PATCH /users/{user} update users.updateDELETE /users/{user} destroy users.destroy
In summary
When using apiResource in Laravel, the routes are typically associated with the api middleware group by default. The api middleware group often excludes unnecessary middleware that is typically used for web requests, which can result in slightly faster performance compared to using the web middleware group.
The api middleware group usually includes middleware like throttle (for rate limiting) and auth:api (for API authentication). These are tailored for API routes, focusing on efficient handling of API requests.
On the other hand, the web middleware group includes middleware that are more suited for traditional web application routes, which might not be necessary or optimal for API routes.
So, if you're building an API and want to optimize for speed and efficiency, using apiResource with the api middleware group is a good choice. It ensures that the routes are handled with middleware optimized for API requests
Original Link: https://dev.to/jonhyknid/laravel-routes-apiresource-vs-resource-ij5

Dev To

More About this Source Visit Dev To