Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 18, 2023 04:07 pm GMT

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

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To