I'm trying to make an unobtrusive action for deleting bookmarks in CakePHP. Allthough it's working just fine, I suspect there must be a better way to do this. Could someone please point me in the right direction?
function delete($id = null) {
$ok = $this->Bookmark->delete($id);
if($this->RequestHandler->isAjax()) {
$this->autoRender = false;
$this->autoLayout = false;
$response = array('status' => 0, 'message' => 'Could not delete bookmark');
if($ok) {
$response = array('status' => 1, 'message' => 'Bookmark deleted');
}
$this->header('Content-Type: application/json');
echo json_encode($response);
exit();
}
// Request isn't AJAX, redirect.
$this->redirect(array('action' => 'index'));
}
If you're planning to use AJAX action calls more extensively, it may be worthwhile to go the "overkill" route, rather than the "inelegant" route. The following method configures your application to handle AJAX requests quite gracefully.
In routes.php, add:
Create a new directory
json
inapp/views/layouts/
, and a new layoutdefault.ctp
in the new directory:Create a new directory
json
inapp/views/bookmarks/
, and a new viewdelete.ctp
in the new directory:Controller:
On the pages from which the AJAX is called, you'd change the AJAX requests from
/bookmarks/delete/1234
to/bookmarks/delete/1234.json
.This also make available to you the option of handling non-AJAX calls to
/bookmarks/delete/1234
with anapp/views/bookmarks/delete.ctp
view.Any further actions that you want to handle via AJAX and JSON, you'd add views in the
app/views/bookmarks/json/
directory.