patternphplaravelMinor
Controller method - Ajax request
Viewed 0 times
requestajaxmethodcontroller
Problem
I'm quite new to Laravel, and I'm not sure what am I doing is the best practice.
I'd like to return JSON if the request is Ajax, or return a view otherwise. This is the way I made it, and it works fine, but I'm not sure if this is the best way(it seems long winded). I'd really like to hear your suggestions.
I'd like to return JSON if the request is Ajax, or return a view otherwise. This is the way I made it, and it works fine, but I'm not sure if this is the best way(it seems long winded). I'd really like to hear your suggestions.
public function store()
{
$input = Input::all();
if(!$this->settings->fill($input)->isValid())
{
if ( Request::ajax() ){
return $this->jsonFailure(array(
'errors' => $this->settings->errors
));
}
else{
return Redirect::route('admin.settings.create')
->withInput()
->withErrors($this->settings->errors);
}
}
$this->settings->save();
if ( Request::ajax() )
return $this->jsonSuccess('success');
else
Redirect::route('admin.settings.index');
}Solution
The only problem with this approach is that as you add more methods, you are going to end up with an awful lot of repeated
Here's what I do for my sites:
(This is not exactly the same as what I am using, but it's pretty close.)
Then, inherit your controllers from
Here is what your example would look like:
Much cleaner!
if (Request::ajax()) ... blocks all over your code.Here's what I do for my sites:
jsonFailure(array(
'errors' => $data
));
} else {
return $this->jsonSuccess($data);
}
} else {
if ($isError) {
return Redirect::route($redirectToIfNotAjax)
->withInput()
->withErrors($data);
} else {
Redirect::route($redirectToIfNotAjax);
}
}
}
}(This is not exactly the same as what I am using, but it's pretty close.)
Then, inherit your controllers from
ApiController instead of just Controller.Here is what your example would look like:
public function store()
{
$input = Input::all();
if(!$this->settings->fill($input)->isValid())
{
return makeResponse($this->settings->errors, true, 'admin.settings.create')
}
$this->settings->save();
return makeResponse('success', false, 'admin.settings.index')
}Much cleaner!
Code Snippets
<?php
class ApiController extends Controller {
protected function makeResponse($data, $isError = false, $redirectToIfNotAjax = '/') {
if (Request::ajax()) {
if ($isError) {
return $this->jsonFailure(array(
'errors' => $data
));
} else {
return $this->jsonSuccess($data);
}
} else {
if ($isError) {
return Redirect::route($redirectToIfNotAjax)
->withInput()
->withErrors($data);
} else {
Redirect::route($redirectToIfNotAjax);
}
}
}
}public function store()
{
$input = Input::all();
if(!$this->settings->fill($input)->isValid())
{
return makeResponse($this->settings->errors, true, 'admin.settings.create')
}
$this->settings->save();
return makeResponse('success', false, 'admin.settings.index')
}Context
StackExchange Code Review Q#57883, answer score: 5
Revisions (0)
No revisions yet.