【问题标题】:Is there a php framework that makes working with jquery & ajax easier?是否有一个 php 框架可以更轻松地使用 jquery 和 ajax?
【发布时间】:2011-06-07 09:04:52
【问题描述】:

在过去的两年里,我一直在使用 Codeigniter,并且真的成为了它的忠实粉丝,但是在过去的一年里,我发现自己编写的 javascript 比 PHP 越来越多。

一开始,我会用 PHP 编写一切,但现在我发现自己一直在使用 $.ajax。而且我有点像我在 javascript 和 php 之间重复自己。

我知道 CI 确实让您可以很好地控制 ajax,但我仍然有两个编写大量的 javascript,如果可能的话,我想巩固一下。

我想我正在寻找的是一个与 jQuery 的 $.ajax 紧密集成的 php 框架。

【问题讨论】:

  • 我无法想象 jQuery 能变得多么容易,它已经让使用 JavaScript 成为了一种儿童游戏。
  • 朋友,你的想象力很枯燥……
  • 您正在寻找这样的东西吗? codeigniter.com/wiki/jQuery 否则我认为您需要指定您的期望。
  • @Peter 也许我会,但是.ajax 方法很简单,如果你发现自己一遍又一遍地编写 .ajax,你可能应该更多地考虑一下你的 JavaScript 应用程序架构。跨度>
  • @Repox,谢谢你的例子......但是那个大红色标题说警告让我害怕......而且,指向课程的链接已损坏:(

标签: php jquery ajax codeigniter frameworks


【解决方案1】:

我在 Javascript 中使用这段代码。后端明智的事物以 MVC 类型的组织形式组织,因此影响一个模块的事物通常组合在一起。一般来说,我也会为单独的模型创建一个单独的模块,但在某些情况下你可能会偏离这个原则。

我的设置是在后面使用 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 之类的东西结合使用,它们会非常好

【讨论】:

  • 好东西!我可能会将其添加到我的工具箱中。谢谢阿伦德!
  • @Peter:如果你要那样做,除了感谢他,你还需要接受他的回答。
  • @Ozair,我会等一下,看看我是否可以对我的问题有更多的了解。谢谢!
【解决方案2】:

查看 Agile Toolkit,它是一个 PHP UI 框架。 UI 意味着它处理 HTML、JavaScript、CSS 和 AJAX,同时允许您使用简单的、面向对象的 PHP 语言进行开发。

http://agiletoolkit.org/intro/javascript

还有一篇博文将其与 CodeIgniter 进行比较:http://agiletoolkit.org/blog/agile-toolkit-for-codeigniter-developer/

附言我是 Agile Toolkit 的合著者。

【讨论】:

  • 这看起来很漂亮!我必须真正深入研究。
  • 非常易于使用,以极少的开销提供如此多的功能。建议。
猜你喜欢
  • 2010-11-19
  • 2015-11-15
  • 2015-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
  • 1970-01-01
相关资源
最近更新 更多