What's the best Ajax library for PHP and why do you prefer it?
If you are searching for a library to facilitate Ajax Requests, it is not dependant of the server-side : launching Ajax requests is done on the client-side, in Javascript.
There are plenty of JS Frameworks to do lots of nice things, and, in the middle of those, Ajax Requests.
I've used those ones :
prototype : the one I know the best ; and really like it
jQuery : probably the most used nowadays
MooTools
On the server-side, the JS Framework used doesn't make much different.
You might still want to take a look at the JSON format, which is great to exchange data between client and server when doing Ajax requests. There are several implementations in PHP that allows you to work with that one.
For instance :
With PHP >= 5.2 : json_encode and json_decode ; nice, as provided by PHP
Zend Framework has Zend_Json ; I've already used it on a PHP 5.1 server, outside of Zend Framework ; nice because I like ZF, and this component works on 5.1
If you want other informations, you might edit your question, to be more clear about what you want :-)
For getting data out of PHP — http://uk3.php.net/json
For abstracting XMLHttpRequest in a cross browser fashion, converting JSON to JavaScript objects, and providing ways to insert that data into a document, I tend towards YUI, but this is largely a matter of personal taste.
It's depends on needs, but JSON is also worth a dig;
http://json.org
Related
I just wondered if there is any existing framework or someone has experiences with using entirely static HTML templates that get filled with data provided via JSON from the server.
The HTML pages with JavaScript support should fulfill the role of the View whereas Controller and Model are handled by the PHP on the server side.
Views would the query via JSON-RPC or similar data from the Model and then render the results by manipulating the DOM.
I pretty much like this idea, it came into my mind weeks ago but I never found something on the web experiences with that or even already existing framework.
But maybe I am just a noob at googling?
Maybe one of you can help me out here.
Thanks in advance
Have a nice day!
Benjamin
This is very much possible and often found with Rich Client Applications. The first call to the application delivers the initial UI with any behavior defined in JavaScript files. Any further interaction is done via Ajax.
For the serverside, have a look at
Zend_Rest_Server
Zend_Json_Server
though this could just as well be a regular MVC application too. A Json or XML Response can still be considered a View. It's up to you to code your controllers to return appropriate data.
That's pretty much how Google do a lot of their stuff - do a view source on Gmail to see how much data they return in the HTML.
It's called Ajax. (That's not meant to sound too sarcastic - Ajax doesn't necessarily work like this, but ajax-based apps will do a lot of the rendering logic via JavaScript rather than via server-side code.)
You need a framework that doesn't limit you to any specific way of handling the view component of your MVC application and I think Zend Framework would be your best choice. It also has a great integration with DOJO and that makes it a lot easier to achieve what you need.
I'm starting to architect a quite complex web application. The implementation is probably going to be done in PHP, though if there are impressive reasons to choose a different environment I might be convinced.
I've looked at tools like Symfony and CakePHP. The problem is that it feels like they're relatively low level for a modern Web 2.0 application. They handle the basic things like MVC and scaffolding, but not the more advanced UI elements that I'm looking for. Here are some of my requirements:
Single page architecture. With minor exceptions, there should be no page refresh. All actions are done via ajax, the way it's done in gmail, and to a lesser extent in Facebook.
Ajax layout and widget handling. Not only the application doesn't refresh the page, but the developer can specify the layout and load various widgets into different parts of the page. This is somewhat like iGoogle, but should be better integrated.
Support both on the client side and server side for AJAX widgets. It should be trivial to display the result of a select statement in an AJAX table/array like http://developer.yahoo.com/yui/datatable/. This should also apply to other widgets including
Trees
Menus
Forms
Speaking of forms, there should be easy integration with client side validation
Signup/Authentication/Authorization. Including all the housekeeping things like forgot my password, CAPTCHAs, etc.
There's more, but I think I've given enough details so that you get an idea for what I'm looking for. Basically, I'd like to engineer a modern Web 2.0 app and skip writing, testing, debugging things that most web applications need to do. And yes, I know I can take YUI or jQuery and slap it on top of one of the regular platforms, but then I'd have to write all the glue. Now if there are modules that do this, that would be interesting.
So if you say, take Symfony + modules xyz + jQuery and there's your answer, I'd be happy to hear that.
Finally, in terms of priority, I'm looking for something that's scalable, reliable, well engineered more than something that's easy to learn and deploy.
Since you are looking for all things AJAX, why not try GWT? Its not PHP I agree, but it makes writing AJAX applications easy for developers.
I agree with #Iznogood. What your looking for is not actually a framework but a talent.You can make any framework as "igoogle-ish" as possible if you know what your doing.
I suggest look for a framework with a huge and active community like, CI, Cake and Jquery. Search/ask the community for the specific things you need. Plug it in and Presto!
But I'm afraid you'll have to write some of it.
If you don't mind using Java, there is ZK:
http://www.zkoss.org/
I'd prefer using this over GWT.
In php, you might want to consider Cjax http://cjax.sourceforge.net/.
It is MVC oriented, and has a very generic API, with full support for customization, including the development of plugins (There is a full Plugin API available, including documentation, Plugins can be built in PHP and JavaScript in combination -- see uploadify and validate plugins).
It can access all JavaScript functions, Objects and proprieties, and elements properties and functions from PHP server side, with one line of code.
In Cjax,
you can execute ajax actions, inside controllers (ajax requests) - without a line of JavaScript. this allows you to fully function without refreshing the page. It also allows you to access all Js objects from PHP. A good sample that reflects this is the "recursive ajax request" see that demo. The API can be used on page load, when the page first loads, and within every single ajax request.
Do take a look at the documentation and to the 20+ demos included, and no where you will see a single line of JavaScript. It allows you to manipulate elements, containers, request ajax, submit ajax forms, creates overlays, upload files through ajax, all from the server side. Take a glace at the API Table.
It has support for both, server side ajax, and client side, (see plugins JavaScript Documentation)
It plays nice with jQuery - the validation plugin in jquery is full executable in PHP without a line of JavaScript (see http://cjax.sourceforge.net/examples/plugin_validate.php without any custom line of Jquery inside the framework, it's all plugin's work). Ajax Responses from Jquery also get processed by this framework automatically, so using Jquery's Ajax function API wise, is the same as if you were using the Framework's Plugin's JavaScript Ajax Functions.
And you would just have to take your time learning more about it, because it has quite a few more tools that I am sure you'd find useful.
Currently there are two official releases, the generic release and
there is an official release for CodeIgnater of this framework (and it is the leading ajax option for codeignater) , and works great in conjunction other PHP Frameworks and without them.
Signup/Authentication/Authorization. Including all the housekeeping
things like forgot my password, CAPTCHAs, etc.
This is something that you can build within an ajax controller, the framework itself its meant to be a generic "AJAX" framework, so if you are looking for none ajax features, you will need to build them or integrate other PHP Framework with Cjax (such as Codeignater).
I do not know if this matches all of your criteria, or if you ever will find one. However, I like the Zend Framework myself.
As for the UI and AJAX portions - the server side technology matters little, and it's more about browser-side technology and interaction mechanisms, as well as DOM manipulation.
jQuery is my favorite for that. As for the PHP back-end, I tend to develop it with my own codebase that's grown with me over the years.
But all this takes a lot of practice, knowledge, education, research, reading, and posting questions on StackOverflow.com ;)
I would recommend symfony PHP framework since it has very good support for every feature you mentioned, and it can be easily integrated with Zend Framework - as of version 2.0 coming this year it would be integrated in the package. You can easily set symfony to handle AJAX requests so that it would be perfect transparent layer handling server side.
For me, if you look at THAT heavy AJAX, I would recommend searching for some good JavaScript framework - such as Ext.JS - that would handle all client side functionalities.
For the front-end part of your app, you might want to have a look at SproutCore ( http://www.sproutcore.com/ ).
Building a Web application with SproutCore feels more like configuring components than writing code.
There is no glue code.
On the backend all you have to do is accept and emit JSON.
Depends what sort of level you're looking at. If by 'framework' you mean something like Zend or Symphony, then to be honest all the big ones are about as good as each other. They all have strong points and weak points, but none will really meet your criteria.
But your question implies you're looking for something more than that kind of framework. Maybe you're looking for a full-blown CMS platform like Drupal, Joomla or Wordpress?
In that case, again, you need to consider your needs verses the capabilities and pitfalls of each system.
Drupal, for example, has masses of modules, is very powerful, and easy to write your own modules, but isn't object-oriented and doesn't really do MVC, so if that's your bag then you may find it hard to get on with. Wordpress is much easier to get going quickly but is less flexible once you start getting deeper into it.
But again, they all have strengths and weaknesses. If one was clearly the best, it would be an easy choice. But at the end of day perhaps it's better to have several good quality options to choose from.
Have a look at Ext JS, it is pretty good. If it is a commercial project you are doing it isn't free but not too expensive either. It also has a GWT version if you prefer that.
This is only for the front end but it is not too complicated to use any backend that can emit json.
CakePHP is pretty good
I have started to try out noSQL databases now and are currently testing out couchDB. Seems like a good solution, but I really get some headache when I follow available examples on how to create views (queries) to select documents from a database and sort them.
Everything I can find is regarding JavaScript and it would be great to take part of some examples for PHP since that is the language we will use.
So, how do I create views using PHP for couchDB?
As far as I know, the equivalent of the SQL language that would be used for a relationnal database is, for CouchDB, Javascript.
So, if you want to create some "query", or "view", you have to do it in that language : it's the one that's understood by CouchDb.
PHP and CouchDb are totally independant : the only thing is that :
PHP can send requests to CouchDb
and get results
But what happens on the CouchDb side is independant of your PHP script -- and there is no PHP on the CouchDb side.
The only way to filter results using PHP would be to :
Request more data from CouchDb than you need
And throw away, from PHP, what's not needed
But, obviously, that would not be an efficient solution...
Javascript is privileged as a CouchDB view server only because that's what's bundled with CouchDB. There are view server implementations for many other languages/runtimes, including PHP.
JavaScript (CouchDB native)
Common Lisp
PHP
Ruby (fork)
Python
couchdb-python
couchdb-pythonviews
Erlang
built-in view server
erlview
Clojure
Perl
Chicken Scheme
Coldfusion
Haskell
OCaml
I think you need to understand Javascript's stance in CouchDB. Javascript isn't a target language like PHP, Ruby, etc. Javascript is more equivalent to SQL here. The javascript is server-side and CouchDB itself actually executes it. This is what you use to do map/reduce and build views and such.
Try PHPillow man
Here's a very short introduction to it: http://www.catswhocode.com/blog/getting-started-with-couchdb-tutorial-a-beginners-guide
If you check out the SVN of PHPillow, there's more information in the 'docs' folder
I realize this is a very generic question, but I guess I'm not really looking for a definitive answer. Being new to PHP frameworks I'm having a hard time getting my head around it.
Javascript frameworks, especially with UI extensions, seem to have their sort-of MVC-like approach by separating your JS code from your design. It just seems like it would get confusing to use an additional MVC framework on the backend.
Is this commonly done for primarily AJAX-driven applications? Is there an accepted/common way of doing it?
It's the next logical step from MVC, in my opinion. You already separate your data access (model), from the business logic (controller), from the output (view) - now you're just separating behaviour from markup.
In my experience, it works really well with AJAX features, since you only need to change your View to return the necessary information as JSON or XML.
A quick example of how it can fit together for a Zend Framework app (and this is from a demo app I wrote a few months ago):
Use the MCV Framework to build a fully functional site (which works without javascript).
Modify the controller to understand the difference between a 'normal' request and an AJAX request (Zend's context switching makes this easy).
Add Javascript (in my example jQuery) to cleanly replace the links with AJAX events.
In the end, the PHP app knows that an AJAX request needs an AJAX response (less bandwidth, less processing, only JSON or HTML 'snippet'), but a normal request needs an entire page generated.
Basically, you're just using AJAX to request (or update, or add data to) the 'view' template, without having to process the entire layout. The Zend Framework Context Switch Action Helper may help this make more sense.
It's worth mentioning that context switching works well in making a request available in different formats - HTML/XML,CSV,etc.
Its a very good idea, since the PHP MVC frameworks are bundled with JS frameworks:
Zend comes with Dojo
Symfony comes with Prototype and script.aculo.us
CakePHP comes with Prototype and script.aculo.us or jQuery (future release)
updated link, thanks "Exception e".
Personally we are using Zend (MVC as well as other aspects of the Zend framework) with jquery and it works very well together. Since not all of your interaction from the html page will be via jquery (ajax) then a standard MVC architecture is highly recommended. You certainly want the layers of your architecture (separating the model and the view) and having jquery is (at least to me) and additional "feature" of being able to execute your MVC asynchronously.
It just seems like it would get confusing to use an additional MVC framework on the backend.
No need to worry about that. You can use zend framework and extjs for example independently while developing, they are really separate products. The dependencies between these layers should be kept simple. No need to worry.
The coupling is low, you only need to set up means to query data from your server-app and do whatever you want with in on the client side. The line between these systems is simple and won't confuse you.
Extjs doesn't really has an mvc structure imho. It offers predefined rich components. You glue these components with some configuration and set up the urls of your server where data can be fetched from.
How do you get your zend mvc respond to ajax? I recommend you to view the presentation about zf ↔ ajax from the zf's project lead.
i am new to flex development my question is How do i pass variables between flex 3 and a mysql database using php? i was told the best way is to AMFPHP but that seems like an over kill or may be not am not sure.. any ideas?
I'd use json over xml since json will transfer less bytes and php's json_encode($object) is quick and easy.
I'd use json over amfphp because json is general purpose. For example, I can create a web service for flex or javascript by returning json.
You might have a look at this article for comparison between json, amfphp, and xml. Also, here is a nice tutorial on flex, php, and json.
AMFPHP is not really being actively developed. The best alternative right now is to use Zend_Amf http://wadearnold.com/blog/?page_id=155, which is supported by both Zend and Adobe.
Don't be scared by the need for Zend Framework components. The framework is modular, and you can use your own custom php classes for accessing data without having to incur the Zend Framework learning curve.
The great thing about using AMF is that since it is a binary data transfer, it's very fast.
Also, working with XML or even JSON, is an annoying extra step if you just want your flex app to get results data an api call. If, for some reason, you need to also handle outputting data to xml or json, that can easily be added to your app by extending or creating new controllers/services that translate the data from arrays and objects to xml or json
There's a few choices open to you. Essentially, it boils down to how you'd like to deal with the data on the PHP side of the fence.
The two I'd spend time investigating would be simple XML (my first preference) and AMFPHP.
XML:
Flex can work very easily with XML data, even mapping it automatically to/from ActionScript objects (generically, or with something like the xobj project on GoogleCode, to typed instances). Similarly, there's plenty of support available for working with XML in PHP code.
AMFPHP:
AMFPHP gives you way to pass typed ActionScript objects over the wire to your PHP code. There's tooling included in the AMFPHP project that makes working with MySQL on the PHP side easy too.
If you are doing a simple query, I would pass your data as plain POST data. You can do this using HTTPService component in Flex. On the PHP side I would respond with an XML string and set the HTTPService resultFormat to "e4x" (Ecmascript for XML), for an easy object-like manipulation of the result data.
Here is the livedocs reference for HTTPService: http://livedocs.adobe.com/flex/3/html/help.html?content=data_access_2.html
For more complicated queries/operations, there is also the option of using the Flex RemoteObject with AMF as the data protocol. On the PHP side you can then use the zend framework AMF component to communicate.
Here is the livedocs reference for RemoteObject: http://livedocs.adobe.com/flex/3/html/help.html?content=data_access_4.html
And the download page for Zend AMF: http://framework.zend.com/download/amf