CakePHP and AJAX
date post
28-Nov-2014Category
Software
view
1.334download
0
Embed Size (px)
description
CakePHP and AJAX - Talk at CakeFest Conference in Madrid 2014
Transcript of CakePHP and AJAX
- 1. CakeFestConference2014-MarkScherer(@dereuromark)
- 2. UsingCakePHPfor6+years(startingwith1.2) CoreDevelopersince2+years ActivebloggeraboutCakePHPissuesandsolutions Germany(Berlin) PassionatePHPandOpenSourceDeveloper
- 3. WhatisAJAX Whentouseit Basics UsingGET UsingPOST Moreexamples Tips
- 4. Loadthewholepageonce. Thenfetchonlypartsortinysnippetsof updatesviafollow-uprequests. Onlyneedsafractionofthe resource/bandwith.
- 5. Dataexchangewithoutchangingposition Inlineediting Realtimevalidation Nativelookandfeel Pre-fetching Datalookups ...
- 6. SEO/Crawling BrowserHistory Bookmarking Networkissues Polling SameOriginPolicy
- 7. GETrequestsshouldbeusedonlyto retrievedata
- 8. /controller/action text/html /controller/action.json application/json URLshouldmatchtheexpectedformat
- 9. //Config/routes.php Router::parseExtensions(); Router::setExtensions(array('json','xml','csv','rss','pdf')); //AppController.php public$components=array('RequestHandler');
- 10. if($this->request->is(array('ajax'))){} $this->request->allowMethod('ajax');
- 11. /** *AJAXactiontogetfavorites * *@returnstring */ publicfunctionfavorites(){ $this->autoRender=false;//Wedon'trenderaviewinthisexample $this->request->allowMethod(array('ajax'));//NodirectaccessviabrowserURL $data=array( 'content'=>..., 'error'=>'', ); $this->response->body(json_encode($data)); } GET/controller_name/favorites.json
- 12. /** *AJAXactiontogetfavorites * *@returnvoid */ publicfunctionfavorites(){ $this->request->allowMethod(array('ajax'));//NodirectaccessviabrowserURL $data=array( 'content'=>..., 'error'=>'', ); $this->set(compact('data'));//Pass$datatotheview $this->set('_serialize','data');//LettheJsonViewclassknowwhatvariabletouse }
- 13.
Clickme
Result:
- 14. $(function(){ $('.ajax-button').click(function(e){ vartargeturl=$(this).data('url'); $.ajax({ type:"get", url:targeturl, beforeSend:function(xhr){ xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded'); }, success:function(res){ if(res.error!=''){ alert(res.error); }else{ $('#target').html(res.content); } }, error:function(e){ alert("FehlerbeiderAnfrage!"); console.log(e); } }); }); });
- 15. $_ENV['HTTP_X_REQUESTED_WITH']='XMLHttpRequest'; $url=Router::url(array( 'controller'=>'controller_name','action'=>'favorites','ext'=>'json', '?'=>array('id'=>2) )); $options=array( 'return'=>'vars' ); $result=$this->testAction($url,$options); ... Makessure$this->request->allowMethod(array('ajax'));passes
- 16. ... data:{'one':$('#one').val(),'two':$('#two').val()}, ... $one=$this->request->data('one'); $two=$this->request->data('two');
- 17. $_ENV['HTTP_X_REQUESTED_WITH']='XMLHttpRequest'; $url=Router::url(array( 'controller'=>'controller_name','action'=>'favorites','ext'=>'json', '?'=>array('id'=>2) )); $options=array( 'data'=>array(...), 'method'=>'post' 'return'=>'vars' ); $result=$this->testAction($url,$options); ... 'data'arraywithmethodtype'post'
- 18. Toggle Autocomplete Chaineddropdowns Pagination
- 19. //AppController.php-e.g.inbeforeRender() if($this->request->is('ajax')){ $this->response->disableCache(); }
- 20. Questions?
- 21. http://book.cakephp.org https://github.com/dereuromark/cakefest2014-app http://sandbox.dereuromark.de/sandbox/ajax_examples http://sandbox.dereuromark.de/sandbox/jquery_examples/ autocomplete http://www.dereuromark.de/2014/01/09/ajax-and- cakephp/ https://github.com/dereuromark/tools