Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a Laravel app where I fetch and update data asynchronously from my frontend. My question is: Do the endpoints for the AJAX requests go into the routes/api.php or in the routes/web.php?

like image 685
F.M.F. Avatar asked Jun 20 '17 13:06

F.M.F.


2 Answers

Normally in web.php as the routes will use the 'web' middleware to access the session and other web-related middleware (CSRF ecc..)

api.php is dedicated to 'stateless' API calls, where you don't want to use session but instead use stateless specific functions such as api authentication throttle and so on

like image 160
Moppo Avatar answered Nov 20 '22 06:11

Moppo


The routes/web.php file defines routes that are for your web interface. These routes are assigned the web middleware group, which provides features like session state and CSRF protection. So generally your all routes having web middleware goes to routes/web.php.

If your route having api middleware then it will goes to routes/api.php.

like image 22
RAUSHAN KUMAR Avatar answered Nov 20 '22 06:11

RAUSHAN KUMAR