I am currently learning jQuery and AJAX and was wondering about best practices for organization of the server-side scripts that handle the AJAX calls. Should I have a handler.php file which then routes the AJAX calls to other functions in different files? Or do you just put everything in one file and use a big switch{}? The system will likely expand in the future and I want to organize it in a way that scales.
A routing file is the way to go, not just for your AJAX requests but for all actions that can be invoked within your system by a web request. You should be placing related actions into a single controller file, and then loading/invoking the correct file/method from your public-facing router.
A good framework, which you absolutely should be using, will do this for you.
What I do is I place the each AJAX call in a function. I name the functions depending on what data I want to retrieve, getUsers(), getComments() etc. Than I call the functions when I need to in JS.
On the PHP side, I use different scripts for each database request. getUsers.php etc...
Related
I want to refactor my php site which supports a front end which makes ajax calls.
Currently my ajax calls a php page which stores all my backend code and based on the parameters passed from the ajaxcall it determines which php methods to run.
I realize building a rest API as the php backend woudl be better but was wondering if there is another alternative? ie a good way to structure a php page that allows for clean re-usable ajax calls from the front end?
Thanks
There are multiple ways of doing such kind of web development. It could be REST based micro framework approach in PHP where you invoke different views. Another could be to invoke the PHP code solely based on the input or interactions on the view (HTML/Javascript) using AJAX. Depends what you want to achieve.
I am creating an application that will make heavy use of Ajax and performance is required. I've searched the best ways to handle Ajax requests using the Zend Framework, and saw that the most commonly used ways are the Context-switch and/or an Action per Controler to handle Ajax requests. If I were to use the first way I would have to keep a script in the view for each type of format that I could return (JSON, XML, etc.), and I think it's unnecessary, and the second way, I would get an action full of if/elses.
I do not see anyone suggesting the creation of a module only for Ajax, this would be a bad practice? In my opinion this would make things easier, and the whole code for Ajax would be in one place, I would like to know what you think about it before making a decision, and of course, constructive criticism is welcome.
If you have a controller which only needs to handle AJAX, then you can just use the views as usual.
The context switch is meant for cases where an action needs to serve multiple types of contents (JSON, XML, HTML etc.).
If you know that the whole controller will just output JSON, for example, you can create a single view (or no view at all) and call this particular view from all actions in the controller.
Im building a little mvc style framework, with a single point of entry through the index in the public folder. I am now passing all my ajax requests through this, and directing them to the appropriate controllers in the private application directory.
What are the pros and cons of doing it like this?
should I do be doing it like this? or another way?
Initially probably not many cons. As Pickle pointed out you have everything in one area so easy to find.
I would say that functionality should stay in the controller where it belongs. Just because the input and output are AJAX shouldn't matter. In fact, you could have a method in a controller like a show user method that could either output an HTML page OR AJAX data. No reason to put that stuff in a separate AJAX controller when it belongs in the User controller.
Honestly this is all design decisions. It's just my preference to keep things where they belong logically and approach AJAX as an input and output problem and build your methods to handle that in/out.
We rolled our own framework and one thing we build was a router that handles incoming traffic. And ajax calls have a .json at the end. This is then available to the controllers and if a controller supports ajax requests it will output valid JSON data instead of passing the data off to the view and then displaying HTML.
Pros: You don't have to search through a bunch of code to find out where your AJAX request is being handled.
Cons: None I can think of.
The only potential downside would be any "expensive" bootstrapping going on that the AJAX doesn't need which may slow down response times. APC may help or negate this, though, depending on what you are doing, and you could probably tailor the bootstrap process to handle lightweight requests.
FWIW, the normal Drupal method for AJAX funnels everything through the same place as normal pages. This is also done to help provide a fallback when the user doesn't have JS enabled.
I have a module setup that uses ajax calls, should I have one file for all my ajax calls or should all the calls have their own file?
Example of what I mean:
index.php ------ With the ajax calls
Should I have one file such as 'ajax.php' that has functions for update, delete, and edit. Or should I have update.php, delete.php, and edit.php?
It all depends on the functions your application will need to complete.
If you will have several functions that edit, several functions that delete, and several functions that update, all functions having separate needs and requirements (thus cannot be made into a single universal function), then separate files for each function (edit_ajax.php, update_ajax.php, etcetera) would be easiest in the long run.
Or, are these Ajax calls only a few of the ones you'll need involving Ajax? In that case, it may be better to group them by their domain function, which is what they're handling (students_ajax.php if those Ajax calls involve updating, deleting and editing students, for example).
Also, make sure your function calls are separated by concern if your application architecture supports it. If these calls are purely sending data to PHP to update your database, keep them in a separate file from the Ajax calls that are merely passing parameters in for a specific set of HTML to load into a page.
Ultimately, your choice is heavily dependent on your application architecture and design style, which may sound more obvious than needed but very true nonetheless.
Depends on how you want to structure it. If you are looking to do page based permissions at any point, having multiple files may be beneficial. However, having all the actions in a single file will make your life a little easier when reading through the code.
You need to pick what is best for your design.
All these actions are db action, so i would just group them in one file. Unless your deleting, updating and editing is tedious, i would give one script the ability to perform all. Otherwise having a one file per action performed seems like going overboard. Generally speaking, you should write your scripts to by dynamic and reusable.
It'll be definitely more convenient for you to have just one entrance point to your program. Put all your operations in one file (like, in index.php) and use switch or whatever logic you want to fire appropriate action. Moreover, even if your have enough logic to separate in to different files or modules, one entrance point still will be nice design decision so you can, for example, perform consistent access control, logging, etc.
I usually use multiple files with one include file that they all use to get all their functions.
That way you get the convenience of a single library, but it's much cleaner and more straightforward having a different file for each.
I would suggest using folders to separate your different pages though. Otherwise your ajax folder gets all cluttered and that can be worse than your code file being cluttered.
I'm building an IMDB.com like website using PHP/jQuery and a MVC approach (no OOP).
I have an index.php base controller to 'rule them all' :), a controllers folder with all the controllers, a models folder and a view folder.
In some pages of the website I have tabbed navigation, when the visitor clicks on one of those tabs to get more information, jQuery gets that data using the $.post or $.get method and shows it on the tab container, obviously without refreshing the page.
The problem is that those pages loaded by ajax are also generated using controllers, models, and views, and the things are getting a bit complicated for someone like me ( = 'no experience'). To dynamically get the data I some times need to include a model twice, include an include in an include in an include, send information multiple times, connect with the database again, and all sort of things like that and I'm sure there is a better and prettier way to do this.
I'm searching for the best approach and common methods for this. I have no experience working with a big project like this. This is a personal project so I have full control and every answer is welcome.
Thanks!!!
You can check the X-Requested-With header that most js frameworks send to see if the request is coming via ajax. Then you can only output certain data and not the "entire page".
Not sure why you need multiple includes like you say, maybe you need to rework your logic.
Post some code and we can be of better help.
"To dynamically get the data I some times need to include a model twice, include an include in an include in an include, send information multiple times, connect with the database again, and all sort of things like that and I'm sure there is a better and prettier way to do this."
I think you need a better 'design' for you MVC application. Multiple includes - i am guessing in the different layers of the MVC framework might be indicators that your design needs more attention.
I'll try and be short:
Other frameworks i.e. handle requests via XMLHTTPRequest by AMONG others, disabling or enabling the VIEW or LAYOUT explicitly - check Zend Framework - (e.g. you need to send a JSON encoded string as a response). These requests are handled just as any other request.
I would suggest you have a look at other popular frameworks. Check the application design and layout, and pay attention on the Routing and Dispatching of Actions. I suggest you follow this path since as you say you lack experience.
good luck with your project.