You are not logged in.

Important Note: This forum is being archived and will be completely locked down on the 7th of February. The new forum can be found at http://laravel.io.

Announcement

    Laravel 4 Advanced Architecture: Book Now On Sale

#1 2012-09-14 19:11:42

JeffreyWay
Artisan
From: Chattanooga, TN
Registered: 2012-05-26
Posts: 28

Best Practice for Responding to AJAX Request?

Hey, guys. Quick question. I'm working on an upcoming Backbone lesson for my course, and I want to make sure that I'm not missing some type of helper that Laravel may provide.

Is this the best way to, in your controller, respond to both regular requests and ones via AJAX?

public function get_index()
{
	$books = Book::all();

	if ( Request::ajax() )
	{
		return Response::eloquent($books);	
	}

	return View::make('book.index')
		->with('books', $books);
}

Would you do it differently? I'm wondering if there's a way to clean this up a bit, as it can quickly get muddy, when, for example, posting to /books to create a new record - something like:

public function post_create()
{
	$is_ajax = Request::ajax();

	$input = $is_ajax ? Input::json() : Input::all();

	$created_new_book = Book::create((array)$input); // temporary

	if ( $is_ajax )
	{
		return json_encode($created);
	}
	else
	{
		return $created_new_book?
			Redirect::to_route('books')->with('flash', 'Book has been added.')
			: Redirect::to_route('new_book')->with('flash', 'Danger, danger Will Robinson.');
	}	
}

Last edited by JeffreyWay (2012-09-14 22:06:18)

Offline

#2 2012-09-14 20:50:11

JeffreyWay
Artisan
From: Chattanooga, TN
Registered: 2012-05-26
Posts: 28

Re: Best Practice for Responding to AJAX Request?

I suppose I could add a filter to help with some of this. That way,

Input::all()

will work, regardless of the type of request.

Route::filter('before', function()
{
	if ( Request::ajax() && Request::method() === 'POST' )
	{
		$input = Input::json();
		Input::replace((array)$input);
	}
});

And then the controller method could be shortened to:

public function post_create()
{
	$created_new_book = Book::create(Input::all()); // temporary

	if ( Request::ajax() ) return json_encode($created_new_book);
	else
	{
		return $created_new_book?
			Redirect::to_route('books')->with('flash', 'Book has been added.')
			: Redirect::to_route('new_book')->with('flash', 'Danger, danger Will Robinson.');
	}	
}

Last edited by JeffreyWay (2012-09-14 22:20:12)

Offline

#3 2012-09-14 21:18:19

JonoB
Artisan
Registered: 2011-11-09
Posts: 284

Re: Best Practice for Responding to AJAX Request?

Above options are fine. You can also create a separate route just for ajax posts if you want.

Either way, you can also return json responses as Response::json($data);

Offline

#4 2012-09-14 21:23:09

Eric Barnes
Moderator
From: North Carolina
Registered: 2011-11-28
Posts: 44
Website

Re: Best Practice for Responding to AJAX Request?

Hi Jeffrey,

Here is what we are doing which is not exactly the same since we are not sharing our backbone stuff with actual web routes. But hopefully it will be of some help anyway. smile

First we have a filter setup:

Route::filter('after', function($response)
{
	if (is_array($response->content) or $response->content instanceof Eloquent)
	{
		$response->content = eloquent_to_json($response->content);
		$response->header('Content-Type', 'application/json');
	}
});

Then a basic controller that responds with backbone:

public function get_index()
{
	return $this->table->get_by('staff_id', Auth::user()->id);
}

public function post_add()
{
	$rules = array(
		'id'  => 'required|integer',
	);
	$content = Input::json();
	$all = (array) $content;
	$v = Validator::make($all, $rules);
	if ($v->fails())
	{
		return Response::error('500');
	}
	// rest of code...
}
public function put_edit($id)
{
	$content = Input::json();
	$all = (array) $content;
	// rest of code....
}

Offline

#5 2012-09-14 21:56:00

JeffreyWay
Artisan
From: Chattanooga, TN
Registered: 2012-05-26
Posts: 28

Re: Best Practice for Responding to AJAX Request?

Hey, Eric - Thanks for the reply! This should help.

Offline

#6 2012-10-20 21:20:59

Damian
Apprentice
Registered: 2012-10-20
Posts: 3

Re: Best Practice for Responding to AJAX Request?

There's a response method for handling the converting to json part in Eric's code.

Route::filter('after', function($response)
{
	if (is_array($response->content) or $response->content instanceof Eloquent)
	{
		$response->content = eloquent_to_json($response->content);
		$response->header('Content-Type', 'application/json');
	}
});

would then become

Route::filter('after', function($response)
{
	if (is_array($response->content) or $response->content instanceof Eloquent)
		$response->content = Response::eloquent($response->content);
});

This method does exactly what Eric's code does though, setting header and using eloquent_to_json function.

Offline

#7 2012-12-03 10:37:37

ghprod
Artisan
From: Indonesia
Registered: 2012-08-16
Posts: 477
Website

Re: Best Practice for Responding to AJAX Request?

Damian wrote:

There's a response method for handling the converting to json part in Eric's code.

Route::filter('after', function($response)
{
	if (is_array($response->content) or $response->content instanceof Eloquent)
	{
		$response->content = eloquent_to_json($response->content);
		$response->header('Content-Type', 'application/json');
	}
});

would then become

Route::filter('after', function($response)
{
	if (is_array($response->content) or $response->content instanceof Eloquent)
		$response->content = Response::eloquent($response->content);
});

This method does exactly what Eric's code does though, setting header and using eloquent_to_json function.

Aha, nice method, i just knew about Respone::eloquent and json big_smile

Offline

Board footer

Powered by FluxBB

');