Peter
Peter

Reputation: 2306

Is there a php framework that makes working with jquery & ajax easier?

I've been using Codeigniter for the past two years and really have become a big fan, but over the past year I've found myself writing more and more javascript than PHP.

In the begining, I would write everything with PHP, but now I find myself using $.ajax all the time. And I sort of feel like Im repeating myself between javascript and php.

I know that CI does give you some good control over ajax, but Im still having two write a ton of javascript and I'd like to consolidate if at all possible.

I guess what I am looking for is a php framework that integrates tightly with jQuery's $.ajax.

Upvotes: 4

Views: 416

Answers (2)

romaninsh
romaninsh

Reputation: 10664

Look into Agile Toolkit, which is a PHP UI Framework. 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

There is also a blog post comparing it with CodeIgniter: http://agiletoolkit.org/blog/agile-toolkit-for-codeigniter-developer/

p.s. I'm co-author for Agile Toolkit.

Upvotes: 3

Arend
Arend

Reputation: 3761

I use this piece of code in Javascript. Backend wise things are organized in a MVC type of organisation, so things affecting one module are usually grouped together. In general I also create a sperate module for a seperate model, but in some cases you may deviate from this principle.

My setup is with symfony at the back and plain jquery at the front. There are some approaches that automatize this part, like http://javascriptmvc.com/, I find it too restricting in many parts. Here is my workflow for integrating php and jquery.

PHP

Execute a piece of code and wrap it inside a try/catch block. This way error messages may be propagated to the frontend. This method helps in that regard to convert exceptions to a readable error. (to debug from 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();

For error handling I often use this to parse errors.

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 side

/**
 * 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');
};

Note: the error callbacks are very nice if you combine them with something like pNotify

Upvotes: 3

Related Questions