CakePHP的和jQuery - 不显眼的行动 [英] CakePHP and jQuery - Unobtrusive actions
问题描述
我试图做一个不显眼的行动,删除在CakePHP的书签。 Allthough它工作得很好,我猜想一定有更好的方法来做到这一点。可能有人请点我朝着正确的方向?
函数删除($ ID = NULL){
$确定= $这个 - > Bookmark->删除($ ID);
如果($这 - > RequestHandler-> isAjax()){
$这个 - > autoRender = FALSE;
$这个 - > =的autoLayout虚假的;
$响应=阵列(状态=大于0,'信息'=>无法删除书签');
如果($行){
$响应=阵列(状态=大于1,'信息'=>'书签'中删除);
}
$这个 - >标题(内容类型:应用程序/ JSON);
回声json_en code($响应);
出口();
}
//请求没有AJAX,重定向。
$这个 - >重定向(阵列('行动'=>'指数'));
}
如果你打算使用AJAX调用的行动更广泛,它可能是值得去的矫枉过正的路线,而不是不雅的路子。下面的方法配置你的应用程序来处理Ajax请求相当优雅。
在routes.php文件,添加:
路由器:: parseExtensions(JSON);
创建 JSON
在应用程序/视图/布局/
,和一个新的布局<$ C $新目录C> default.thtml中在新目录中:
&LT; PHP
标题(杂注:无缓存);
标题(缓存控制:无店面,无缓存,最大年龄= 0,必重新验证);
标题(内容类型:text / X-JSON);
标题(X-JSON:$ content_for_layout。);
回声$ content_for_layout;
?&GT;
创建 JSON
在应用程序/视图/书签/
,和一个新视图<$ C $新目录C> delete.ctp 在新目录中:
&LT; PHP
$响应= $确定
?阵列(状态=大于1,'信息'=&GT; __(书签删除,真正的))
:数组(状态=大于0,'信息'=&GT; __('无法删除书签,真));
回声$ Javascript成为&GT;对象($响应); //数组成JSON对象转换。
?&GT;
控制器:
类BookmarksController扩展AppController的()
{
变量$组件=阵列(RequestHandler的');
功能beforeFilter()
{
父母:: beforeFilter();
$这个 - &GT; RequestHandler-&GT; setContent(JSON,文本/ X-JSON);
}
函数删除($ ID)
{
$确定= $这个 - &GT; Bookmark-&GT;德尔($ ID);
$这个 - &GT;集(紧凑型($ OK));
如果($这个 - &GT;!RequestHandler-&GT; isAjax())
$这个 - &GT;重定向(阵列('行动'=&GT;'指数'),303,真正的);
}
}
在从中AJAX调用的页面,你会改变从的AJAX请求/书签/删除/ 1234
到 /书签/删除/ 1234.json
。
这也提供给你处理非Ajax的选项调用 /书签/删除/ 1234
与应用程序/视图/书签/delete.ctp
视图。
这要通过AJAX和JSON处理任何进一步的行动,你会添加视图在应用程序/视图/书签/ JSON /
目录。
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:
Router::parseExtensions('json');
Create a new directory json
in app/views/layouts/
, and a new layout default.ctp
in the new directory:
<?php
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate");
header('Content-Type: text/x-json');
header("X-JSON: ".$content_for_layout);
echo $content_for_layout;
?>
Create a new directory json
in app/views/bookmarks/
, and a new view delete.ctp
in the new directory:
<?php
$response = $ok
? array( 'status'=>1, 'message'=>__('Bookmark deleted',true))
: array( 'status'=>0, 'message'=>__('Could not delete bookmark',true));
echo $javascript->object($response); // Converts an array into a JSON object.
?>
Controller:
class BookmarksController extends AppController()
{
var $components = array('RequestHandler');
function beforeFilter()
{
parent::beforeFilter();
$this->RequestHandler->setContent('json', 'text/x-json');
}
function delete( $id )
{
$ok = $this->Bookmark->del($id);
$this->set( compact($ok));
if (! $this->RequestHandler->isAjax())
$this->redirect(array('action'=>'index'),303,true);
}
}
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 an app/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.
这篇关于CakePHP的和jQuery - 不显眼的行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!