Laravel: AJAX request endpoints in /api.php or routes / web.php routes?

I have a Laravel application in which I retrieve and update data asynchronously from my interface. My question is: do endpoints end up for AJAX requests in routes/api.phpor in routes/web.php?

+6
source share
2 answers

Usually in web.php, because routes will use the web middleware to access the session and other Internet-related middleware (CSRF ecc ..)

api.php designed for stateless API calls in which you do not want to use a session, but instead use non-standard functions such as api authentication throttle, etc.

+6
source

The file routes/web.phpdefines the routes that are intended for your web interface. These routes are assigned a middleware group webthat provides features such as session stateand CSRF protection. Therefore, usually your routes with middleware webare sent to routes/web.php.

If your route has apimiddleware, then it will go to routes/api.php.

+1
source

All Articles