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

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

In the beginning I wrote everything with PHP, but now I use $ .ajax all the time. And it seems to me that I am repeating between javascript and php.

I know that CI really gives you good control over ajax, but I still have two tons of javascript entries, and I would like to consolidate, if at all possible.

I suppose I'm looking for a PHP framework that integrates closely with jQuery $ .ajax.

+4
source share
2 answers

I am using this piece of code in Javascript. Backend-wise things are organized in an organization like MVC, so things affecting one module are usually grouped together. In general, I also first create a module for a separate model, but in some cases you can deviate from this principle.

My setup with symfony on the back and simple jquery in the front. There are several approaches that automate this part, for example http://javascriptmvc.com/ , and I find it too limited in many parts. Here is my workflow for integrating php and jquery.

Php

Execute part of the code and wrap it in a try / catch block. Thus, error messages can be transmitted to the external interface. This method helps to convert exceptions into a readable error in this regard. (for debugging 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 analyze 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: error callbacks are very nice if you combine them with something like pNotify

+3
source

Take a look at the Agile Toolkit, which is the PHP UI Framework . The user interface means that it takes care of HTML, JavaScript, CSS and AJAX, allowing you to develop a simple, object-oriented PHP language.

http://agiletoolkit.org/intro/javascript

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

ps I am a co-author of the Agile Toolkit.

+3
source

All Articles