Recommend this page to a friend! |
Classes of El Amrani Chakir | PHP AJAX Response Handler | README.md | Download |
|
Download ![[preview]](https://socialify.git.ci/AmraniCh/ajax-dispatcher/image?description=1&descriptionEditable=Handle%20AJAX%20requests%20and%20send%20them%20to%20an%20appropriate%20handler.%20Also%20provides%20helper%20classes%20to%20simulate%20HTTP%20requests%20and%20responses.&font=Raleway&owner=1&pattern=Charlie%20Brown&theme=Light) Getting Started
You can copy/paste this code snippet for a quick start.
UsageRoute to controller/class methodIf you like to put the business logic in a separate class or in a controller, you can route your requests to them like this :
Or :
But you have after to register the controller namespace/instance in the router, like this :
If the controller/class has some dependencies that must be passed within the constructor, you can still instantiate the controller on yourself :
Catch handlers exceptions*I want to catch exceptions that only occurs from my AJAX handlers, and not those thrown by the library or somewhere else, how I can do that ?* Answer :
Clean output buffer before calling requests handlers (Recommended)To be sure that the response (output) for the ajax request it comes only from the defined handlers, and prevent
unexpected echos from sending their content earlier to the browser use the
Exit the script after dispatching the request (Recommended)Use
<hr> Security tips for production servers1. Check for the request if it is an "AJAX request"
Note : The 2. Check for your own URLIf you want to add an extra layer of security to your application, you can check for the
Note : HTTP headers can be spoofed, that means the content of the 3. Using CRSF tokensGenerate the CRSF token :
Inject the token somewhere in your page, for example in a meta tag :
Send the token in every AJAX request that you made :
Complete example for production usageI already implemented all these security tips in one example, if you want to check it out see the examples/production folder. <hr> BackgroundThe idea of the library came to my mind a long time ago when I was mostly developing web applications using just plain PHP, some of these applications were performing a lot of AJAX requests into a single PHP file, that file can have a hundred lines of code that process these requests depending on a function/method name that send along with the request, so I started to think of ways to clean up this file to improve the readability and maintainability of the code. Buy me a coffee<a href="https://www.buymeacoffee.com/AmraniCh" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a> They support meA special thanks to JetBrains company for their support to my OSS contributions. <img width="150px" src="https://resources.jetbrains.com/storage/products/company/brand/logos/jb_square.png"/> LICENSEThe library is licensed under the open source MIT licence. |