Jump to content

Recommended Posts

Is it just me or does ajax complicate the application architecture?

I am building a framework that will be used to create my vision of the ultimate content management but unlike other content management systems its unique in the way its implemented making it more dynamic but the reason I bring it up is I have chosen to leave ajax out of the main design because I wanted a front controller. I wanted it so everything goes through the one index file and so much so that i currently have mod rewrite setup so any file that is not an image or css js or xml will be sent straight to index.php The idea of having to manage both ajax input and output and the controller input and output is not something I want to maintain for simplicity.

One of the reasons i chose to keep ajax out of the basic page reload was because of headers. I want my system i am developing to have

groups\titles\ranks\default

and based upon the values of the selected page the page will have different keywords in the html head the point is that in order to get the keywords i select the page. If i went with an ajax approach the problem flow in my mind would be harder to maintain in terms of the head because they rely so much on the page information that i get when i load\select the page there are other little things as to why i don't want to do it both ways. For example I don't like the idea of having to load my framework twice. Once for my index.php and once for my ajax.php.

Its because of many of these things I have dismissed ajax as only being useful for things such as games and widgets and programs that require the ability to do things such as a chat script in terms of request and response.

My question is just this. Is there something I am missing. I want to hear how each person here implements ajax in their design suggestions and how you do it in terms of architecture is what I want to hear. I am at a point where I am writing things such as form generators for my framework and i am undecided on if and how i would implement ajax and if how i would do both. so I have had to revist ajax :(
Link to comment
https://forums.phpfreaks.com/topic/34067-ajax-integration-questions/
Share on other sites

i actually "borrowed" the idea from Cake here. They structure their URI's in a /class/method/id/ type way, but also allow stuff like /bare/class/method/id/ and /ajax/class/method/id/ etc which doesnt concern itself with regular layouts. Their complete layouts are made in two parts - the "master" template (or layout file), and the inner template which is method specific.

my own framework uses this method too for a few things that need to be able to access the models, helpers, libraries, etc in the same way as a regular call, but without the extra overhead. The only difference with prepending 'ajax' to the URI is that the "master" layout file is totally ignored and the method-specific template is only used if specifically told to, whereas with a normal call, it would be required and included automatically. make sense?

in my case, rendering the actual HTML from the controller is actually the longest part of the process, so simply cutting this bit out allows me to still serve an AJAX page, but with pretty minimal overhead.

I actually treat images/files the same way. Rather than create a seperate script that doesnt use my framework to make the DB connections, serve the files, etc, I actually have URI's such as /image/thisfile.jpg/100/100/crop/ (the numbers being option, max width, height, etc, followed by an option 'scale' method). Again, whilst unrelated to AJAX, literally everything goes through the framework (albeit the difference with images/files is the whole process gets stopped short of invoking the controller).

fingers crossed that made some sense ;)

[b]edit[/b]: a good example that does a similar thing is here: http://video.derekallard.com/ for the CodeIgniter framework, but it should give you a good idea of how they integrate AJAX into the scheme of things.

hope it helps!
cheers
Yes that idea of /class/method/ i chose not to do for my project. Instead I have a class called page that is abstract and my extended version is required to add abstract functions such as

[code]
function _config() {
}

function get_page($page_name) {
}

function not_found($page_name) {
}

function loaded() {
}
[/code]

the idea is that i work based off the return values off these abstract functions. the return values should be arrays and i can set errors into my error object that i hide in the registry. The way I do it is not compatible with /class/method/some_var/some_val

I use a controller class just like you to make my vars like the below just like you
/some/var/some_array/one+two+three
/index.php/some/var/some_array/one+two+three

I have just been thinking that no matter what I do the http_request is really like opening a second page behind and putting the result in the javascript from the first page so no matter what i do stuff will have to be reloaded on the second page. that really sucks :(

I currently have a /pages/ dir for my controllers and /templates/default/ for views I think I will add two subdirs to pages so I can have ajax and normal pages combined with a separate file as an ajax controller that loads the framework a second time.

By loading the framework a second time I am not referring to my page\display I am thinking about database connections and such. It is something I really wanted to avoid But I did some more thinking about it and i dont get how I can get around making a second object for the databases and so on as its a second http_request.

The only part I think i need to figure out now is loading the database and all my other objects from my framework that make life easier in a way that gives best performance. :(
I do personally think that (whilst there's alot of overhead issues with Cake other than AJAX) they have a nice way of doing it that keeps the performance hit down pretty well.
I'm talking away from any "norm" or hard research here, but I dont see too much harm in invoking the framework for a second time. Depending on how your dispatcher/router works could make some decisions, based on the type of request, of exactly how much legwork to do. When I strip away all the normal crap during an AJAX call, there's really not much difference compared to a regular application AJAX call, but comes with the benefits that the PHP files invoked via AJAX can access my framework in just the same way, keeping everything nice and tight. I guess it just depends.
I've toyed recently with storing a Registry object in a session, which holds all of my persistent info (including DB/config/etc info) and it seems to do a good job, although I've not played around enough to commit to this way as yet.
I guess it's all food for though, and yeah - AJAX does throw a bit of a spanner in the works in terms of maintaining a nice clean structure, but from what i've tried out so far incorporating it in the framework just like a normal page request, it does the job nicely.
The problem I am thinking is with things that poll the server for example or lets just say i do multiple ajax requests thats a new load of the framework each time. The problem is not that it wouldnt work but its one of those things that annoys me. I am a perfectionist and I see it as not very efficient code. I am going to try to look into serializing something like say $DB and encrypting it in a way that can be passed as post data. That way I might not have to reparse my config files and reconnect to all the databases in my system. (my db class is setup to load every db in the config files after its loaded for the first time)

Some sort of serialization wtih POST will be required. Maybe with a

require_once '/libs/ajax.php'; //load ajax part that unserializes any serialized objects followed by whatever code i want??

I may or may not incorporate it into the main design. In some ways it is simpler to separate part of it and in other ways its simpler to use control statements with exit();

If you have any ideas about serializing a DB object in a way that can be sent via post let me know. I have never tried it before the only thing I might be worried about is i know some characters cannot be sent via _GET and _POST so if i can encrypt it in a way that is accepted by POST then i should be right. If that is the case I think I will instead pass objects to an ajax file similar to the way you do with a template engine

$ajax = new Ajax('button');
$ajax->onclick = 'some_call';
$ajax->assign('DB', DB('postgres'))
$ajax->assign('text', 'some_text');

$string =  $ajax->finish();

or <?=$ajax?>

maybe something like that that sends DB=serialized_data_here&text=some_text

I just tried a basic example of what i described in my above post and it does work. I can pass objects just fine but there is one problem. PDO throws an exception when you try to serialize it. But regardless of how you look at it I can pass objects such as the config object that alone stops me having to reparse all my config files (i have more than one)

If anyone knows of any way to serialize pdo objects plz let me know.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.