{"id":17130046,"url":"https://github.com/serkanyersen/ajaxhandler","last_synced_at":"2025-04-13T07:23:46.761Z","repository":{"id":2722624,"uuid":"3717329","full_name":"serkanyersen/AjaxHandler","owner":"serkanyersen","description":"ASimple PHP Class to help handling Ajax Requests easily","archived":false,"fork":false,"pushed_at":"2012-06-18T15:04:14.000Z","size":200,"stargazers_count":31,"open_issues_count":0,"forks_count":5,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-26T23:35:22.565Z","etag":null,"topics":["ajax","ajax-handler","ajax-request","callback","jsonp","php","request","utility"],"latest_commit_sha":null,"homepage":"http://serkanyersen.github.com/AjaxHandler","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/serkanyersen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-03-14T12:22:29.000Z","updated_at":"2024-02-14T10:48:24.000Z","dependencies_parsed_at":"2022-09-03T15:02:01.229Z","dependency_job_id":null,"html_url":"https://github.com/serkanyersen/AjaxHandler","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serkanyersen%2FAjaxHandler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serkanyersen%2FAjaxHandler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serkanyersen%2FAjaxHandler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serkanyersen%2FAjaxHandler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/serkanyersen","download_url":"https://codeload.github.com/serkanyersen/AjaxHandler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248676646,"owners_count":21143938,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ajax","ajax-handler","ajax-request","callback","jsonp","php","request","utility"],"created_at":"2024-10-14T19:11:19.393Z","updated_at":"2025-04-13T07:23:45.952Z","avatar_url":"https://github.com/serkanyersen.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"### Don't write that `switch` again.\nAre you tired of writing the same Ajax handler code over and over again on your each project? There are always the same steps and same problems, is it handling headers correctly? Will it work with jQuery? Status Codes? JSONP? \n\nWell, I've collected all the code pieces together and created the AjaxHandler. It's a very simple PHP Class which you can inherit and create your own Ajax handlers easily.\n\n### Features\nHere is a list of features Ajax Handler provides\n\n*   __Unified Output__ Your every Ajax request will produce a standard JSON output, so your client code will be simpler and understandable. Outputs are only handled by ::error and ::success methods, You'll never have to use `json_encode` when outputting.\n*   __Error Handling__ AjaxHandler will automatically catch exceptions and PHP errors and handle them for you. You can customize error messages and changes status codes\n*   __JSONP Support__ Ajax handler will automatically handle requests with callback parameters, you can customize the callback name and type\n*   __Clean Code__ Object Oriented design will let you easily tear apart your code into separate files, This is especially good for Backbone like MVC projects\n*   __Stats__ Ajax handler will keep duration of your each request and return it to you, so you can easily measure your codes performance\n*   __Very Simple Code__ Ajax Handler's code is very simple and you can easily customize it according to your needs.\n*   __It's All Free__ Ajax Handler is an MIT licensed piece of code so you can use it own your own projects\n\n\nHere is a very basic example of AjaxHandler in Use lets say this is person.php\n\n```php\n\u003c?php\ninclude \"AjaxHandler.php\";\nclass Person extends AjaxHandler{\n    \n    /**\n     * Private function will not be accessed from outside\n     * @return Mongo Collection\n     */\n    private function getMongo(){\n        $m = new Mongo();\n        $db = $m-\u003eselectDB('main');\n        $cl = $db-\u003eselectCollection('people');\n        return $cl;\n    }\n\n    /**\n     * All I write here is the code\n     * I didn't write anything to handle output\n     * If the code gives an exception, ajax \n     * handler will return error, If Everything is successfull\n     * Standart success message will be returned\n     * @return [type] [description]\n     */\n    public function createPerson(){\n        \n        $db = $this-\u003egetMongo();\n        \n        $result = $cl-\u003esave(array(\n            \"name\" =\u003e $this-\u003eget('name'),\n            \"age\"  =\u003e $this-\u003eget('age')\n        ));\n    }\n    \n    /**\n     * Here is the code for handling your own messages\n     * @return [type] [description]\n     */\n    public function getPersonDetails(){\n        $db = $this-\u003egetMongo();\n\n        $cursor = $db-\u003efetch(array('name' =\u003e $this-\u003eget('name')));\n\n        if($cursor-\u003ecount() === 0){\n            // Will produce an error\n            // {\"success\": false, \"error\": \"Person cannot be found\"}\n            $this-\u003eerror('Person cannot be found');\n        }else{\n            // Will giveout a JSON success message\n            // {\n            //   \"success\": true, \n            //   \"details\":{\"name\":\"john\", \"age\":\"29\"}, \n            //   \"message\":\"Operation successful\"\n            // }\n            $this-\u003esuccess(array(\n                \"details\"=\u003e$cursor-\u003efirst()\n            ));\n        }\n    }\n}\n?\u003e\n```\n\nYou can make requests with jQuery easily\n\n```javascript\n$.ajax({\n    url:'person.php',\n    data:{\n      action: 'createPerson',\n      name: 'john',\n      age: 29\n    },\n    dataType:'json',\n    complete: function(res){\n        if(res.success){\n            alert('User Saved');\n        }else{\n            alert('Error: '+ res.error);\n        }\n    }\n});\n```\nI'll write a more detailed documentation soon.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserkanyersen%2Fajaxhandler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fserkanyersen%2Fajaxhandler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserkanyersen%2Fajaxhandler/lists"}