Laravel Restfull controllers and ajax / sync request routing

I am looking for the most efficient way to handle both ajax requests in the form of synchronization requests using the usual form. As far as I know, there are two processing methods, for example, a new order request:

Option 1: AJAX checks in the controller (check and save to the left for simplicity).

//Check if we are handling an ajax call. If it is an ajax call: return response //If it a sync request redirect back to the overview if (Request::ajax()) { return json_encode($order); } elseif ($order) { return Redirect::to('orders/overview'); } else { return Redirect::to('orders/new')->with_input()->with_errors($validation); } 

In the above case, I have to do this check in every controller. The second case solves the problem, but for me it seems redundant.

Option 2: allow the router to handle request validation and assign controllers based on the request.

 //Assign a special restful AJAX controller to handle ajax request send by (for example) Backbone. The AJAX controllers always show JSON and the normal controllers always redirect like in the old days. if (Request::ajax()) { Route::post('orders', ' ajax.orders@create '); Route::put('orders/(:any)', ' ajax.orders@update '); Route::delete('orders/(:any)', ' ajax.orders@destroy '); } else { Route::post('orders', ' orders@create '); Route::put('orders/(:any)', ' orders@update '); Route::delete('orders/(:any)', ' orders@destroy '); } 

The second option seems to me more understandable from the point of view of routing, but this is not from the point of view of the workload (interaction with the model, etc.).

Solution (by thinkers)

The thinkers' answers were in place and decided this for me. Here are some more details in the extension of the Controller class:

  • Create the controller.php file in the application / libraries.
  • Copy the controller extension code with the thinkers' answers.
  • Go to application / config / application.php and comment on this line: 'Controller' => 'Laravel \ Routing \ Controller',
+4
source share
1 answer

A solution that I left on the Laravel forum includes an extension to the Core controller class to manage ajax and non ajax requests for a REST-based system. Instead of checking your routes and switching based on the request transport, you simply add some functions to your controllers with the prefix 'ajax_' . So, for example, your controller will have functions

 public function get_orders() { will return results of non-ajax GET request} public function ajax_get_orders() { will return results of ajax GET request } public function post_orders() {will return results of non-ajax POST request } public function ajax_post_orders() { will return results of ajax POST request } 

and etc.

Here you can find the pasta

To extend the Core Controller class, you need to change the Controller class in the /config/application.php application, then set the $ajaxful in the controller class to true (and $restful also if you want restuful ajax controllers).

+6
source

All Articles