[英]Is there a php framework that makes working with jquery & ajax easier?
在过去的两年里,我一直在使用 Codeigniter,并且真的成为了它的忠实粉丝,但在过去的一年里,我发现自己写的 javascript 比 PHP 越来越多。
一开始,我会用 PHP 编写所有内容,但现在我发现自己一直在使用 $.ajax。 我觉得我在 javascript 和 php 之间重复自己。
我知道 CI 确实让你对 ajax 有一些很好的控制,但我仍然有两个写了大量的 javascript,如果可能的话,我想巩固一下。
我想我正在寻找的是一个与 jQuery 的 $.ajax 紧密集成的 php 框架。
我在 Javascript 中使用了这段代码。 后端明智的事物以 MVC 类型的组织形式组织,因此影响一个模块的事物通常组合在一起。 一般来说,我也会为单独的 model 创建一个单独的模块,但在某些情况下你可能会偏离这个原则。
我的设置是后面的 symfony 和前面的普通 jquery。 有一些方法可以使这部分自动化,比如http://javascriptmvc.com/ ,我发现它在很多部分都受到限制。 这是我集成 php 和 jquery 的工作流程。
PHP
执行一段代码并将其包装在 try/catch 块中。 这样错误消息可能会传播到前端。 在这方面,此方法有助于将异常转换为可读错误。 (从 json 调试)。
try {
//... execute code .. go about your buisness..
$this->result = "Moved " . count($files) . " files ";
// result can be anything that can be serialized by json_encode()
} catch (Exception $e) {
$this->error = $e->getMessage() . ' l: ' . $e->getLine() . ' f:' . $e->getFile();
// return an error message if there is an exception. Also throw exceptions yourself to make your life easier.
}
// json response basically does something like echo json_encode(array("error" => $this->error, "result" => $this->result))
return $this->jsonResponse();
对于错误处理,我经常使用它来解析错误。
public function parseException($e) {
$result = 'Exception: "';
$result .= $e->getMessage();
$trace = $e->getTrace();
foreach (range(0, 10) as $i) {
$result .= '" @ ';
if (!isset($trace[$i])) {
break;
}
if (isset($trace[$i]['class'])) {
$result .= $trace[$i]['class'];
$result .= '->';
}
$result .= $trace[$i]['function'];
$result .= '(); ';
$result .= $e->getFile() . ':' . $e->getLine() . "\n\n";
}
return $result;
}
Javascript侧
/**
* doRequest in an ajax development tool to quickly execute data posts.
* @requires jQuery.log
* @param action (string): url for the action to be called. in config.action the prefix for the url can be set
* @param data (object): data to be send. eg. {'id':5, 'attr':'value'}
* @param successCallback (function): callback function to be executed when response is success
* @param errorCallback (function): callback function to be executed when response is success
*/
jQuery.doRequest = function (action, data, successCallback, errorCallback) {
if (typeof(successCallback) == "undefined") {
successCallback = function(){};
}
if (typeof(errorCallback) == "undefined") {
errorCallback = function(data ){
alert(data.error);
};
}
jQuery.log(action);
jQuery.post(action, data, function (data, status)
{
jQuery.log(data);
jQuery.log(status);
if (data.error !== null || status != 'success') {
// error handler
errorCallback(data);
} else {
successCallback(data);
}
},'json');
};
注意:如果将它们与pNotify之类的东西结合使用,错误回调非常好
查看 Agile Toolkit,它是一个PHP UI 框架。 UI means it takes care of HTML, JavaScript, CSS and AJAX while allowing you to develop in plain, object-oriented PHP language.
http://agiletoolkit.org/intro/javascript
还有一篇博文将其与 CodeIgniter 进行比较: http://agiletoolkit.org/blog/agile-toolkit-for-codeigniter-developer/
ps 我是 Agile Toolkit 的合著者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.