Guest User

Untitled

a guest
Jul 25th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Test controller extending Controller_Cambiata_REST
  5. * Jonas Nyström - cambiata
  6. *
  7. * To be used with a route like this one:
  8. *
  9. * Route::set('rest', 'rest(/<par1>(/<par2>(/<par3>(/<par4>(/<par5>)))))(.<format>)')
  10. * ->defaults(array(
  11. * 'controller' => 'testrest',
  12. * 'action' => 'index',
  13. * 'format' => 'xml',
  14. * ));
  15. *
  16. */
  17.  
  18. class Controller_TestRest extends Controller_Cambiata_REST {
  19.  
  20. private $parameter1;
  21. private $parameter2;
  22. private $parameter3;
  23.  
  24. public function before() {
  25. parent::before();
  26.  
  27. //------------------------------------------------------------------------------
  28. // Get some request parameters
  29.  
  30. $this->parameter1 = $this->request->param('par1');
  31. $this->parameter2 = $this->request->param('par2');
  32. $this->parameter3 = $this->request->param('par3');
  33.  
  34. //------------------------------------------------------------------------------
  35. // Init authentication
  36. //Cambiata_HttpAuth::basic_http_auth($this, 'basic_auth_callback');
  37. // or...
  38. //Cambiata_HttpAuth::header_token_auth($this, 'token_auth_callback', Cambiata_HttpAuth::X_AUTH_TOKEN);
  39. }
  40.  
  41. //-----------------------------------------------------------------------------
  42. // Authentication callbacks
  43. /*
  44. static public function basic_auth_callback($username, $password) {
  45. // this method should be overridden
  46. return true; //($username == 'jonas' AND $password = 'cambiata');
  47. }
  48. */
  49. // or...
  50. /*
  51. static public function token_auth_callback($token) {
  52. // this method should be overridden
  53. return ($token == 'abc123');
  54. }
  55. */
  56.  
  57. //-------------------------------------------------------------
  58. //-------------------------------------------------------------
  59. //-------------------------------------------------------------
  60.  
  61. // GET
  62. public function action_index(){
  63.  
  64. // Create some output data
  65. $output_data = array('Hello' => 'World!');
  66.  
  67. $this->rest_output($output_data);
  68. }
  69.  
  70. // POST
  71. function action_create()
  72. {
  73. // Do whatever needed with incoming data:
  74. // $this->input_content -> insert to database or something...
  75.  
  76. // Create some output data
  77. $output_data = new StdClass();
  78. $output_data->monitor_incoming = $this->input_content;
  79. $output_data->test = 'ABC123';
  80. $output_data->hobbies = array('Knitting', 'Cliffhanging');
  81.  
  82. $this->rest_output($output_data);
  83. }
  84.  
  85. // PUT
  86. function action_update()
  87. {
  88. // Do whatever needed with incoming data:
  89. // $this->input_content -> update database or something...
  90.  
  91. $this->rest_output('PUT');
  92. }
  93.  
  94. // DELETE
  95. function action_delete()
  96. {
  97. $this->rest_output('DELETE');
  98. }
  99.  
  100.  
  101. }
Add Comment
Please, Sign In to add comment