@
@
@Twitt2018-04-14 12:13:20
Laravel
@Twitt, 2018-04-14 12:13:20

Where is the right place to send AJAX requests in Laravel?

I worked on a project, we created an api folder, created a php file in it that will process an AJAX request and return a response from there.
What is the correct way to do this in Laravel? In the view, write JS that will send an AJAX request, and there are the following options:

  1. Create an AjaxController that will accept requests from everywhere and process them there
  2. Create an api folder as I described above and create a file that will process the ajax request

Please advise which option is correct. Thanks

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Koteezy, 2018-04-14
@Koteezy

In the routes/api.php folder, add your routes,
and you can put the controllers that will process them in app/http/controllers/api/*

H
hakkol, 2018-04-14
@hakkol

Why put all ajax methods or routes in one place? If you want to request something from the users table via ajax, then create a method in UserController. In the method, you can specify, for exampleif (!$request->ajax()) return abort(403);

Y
Yan-s, 2018-04-14
@Yan-s

Laravel and php does not matter AJAX is a request or not, the principle of operation is exactly the same. Just create a route and a controller method for a specific request. If you need to somehow process all ajax calls in the same way, then there is a middleware for this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question