├── .gitmodules ├── LICENSE ├── README.md ├── application ├── Bootstrap.php ├── configs │ └── application.ini └── modules │ ├── api │ ├── Bootstrap.php │ ├── controllers │ │ ├── ErrorController.php │ │ ├── FooController.php │ │ └── IndexController.php │ └── views │ │ └── scripts │ │ └── index │ │ └── index.phtml │ └── default │ ├── Bootstrap.php │ ├── controllers │ ├── ErrorController.php │ └── IndexController.php │ └── views │ └── scripts │ ├── error │ └── error.phtml │ └── index │ └── index.phtml └── public ├── .htaccess └── index.php /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "library/REST"] 2 | path = library/REST 3 | url = git://github.com/codeinchaos/restful-zend-framework.git 4 | [submodule "library/Zend"] 5 | path = library/Zend 6 | url = git://github.com/codeinchaos/zend-framework.git 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011 Code in Chaos Inc. http://www.codeinchaos.com 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RESTful Applications with Zend Framework [Example] 2 | 3 | This is an **example** to demonstrate the usage of [RESTful Zend Framework Extension](https://github.com/ahmadnassri/restful-zend-framework) which allows to create RESTful Controllers with ease. 4 | 5 | for implementation details please refer to [github.com/ahmadnassri/restful-zend-framework](https://github.com/ahmadnassri/restful-zend-framework). 6 | -------------------------------------------------------------------------------- /application/Bootstrap.php: -------------------------------------------------------------------------------- 1 | setRequest(new REST_Request); 10 | $frontController->setResponse(new REST_Response); 11 | 12 | // add the REST route for the API module only 13 | $restRoute = new Zend_Rest_Route($frontController, array(), array('api')); 14 | $frontController->getRouter()->addRoute('rest', $restRoute); 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /application/configs/application.ini: -------------------------------------------------------------------------------- 1 | [production] 2 | phpSettings.display_startup_errors = 0 3 | phpSettings.display_errors = 0 4 | 5 | includePaths.library = APPLICATION_PATH "/../library" 6 | 7 | bootstrap.path = APPLICATION_PATH "/Bootstrap.php" 8 | bootstrap.class = "Bootstrap" 9 | 10 | appnamespace = "Application" 11 | autoloaderNamespaces[] = "REST_" 12 | 13 | resources.frontController.params.displayExceptions = 0 14 | resources.frontController.moduleDirectory = APPLICATION_PATH "/modules" 15 | resources.frontController.params.prefixDefaultModule = true 16 | 17 | resources.modules[] = "" 18 | 19 | rest.default = "json" 20 | rest.formats[] = "xml" 21 | rest.formats[] = "json" 22 | 23 | [staging : production] 24 | 25 | [testing : production] 26 | phpSettings.display_startup_errors = 1 27 | phpSettings.display_errors = 1 28 | 29 | [development : production] 30 | phpSettings.display_startup_errors = 1 31 | phpSettings.display_errors = 1 32 | resources.frontController.params.displayExceptions = 1 33 | -------------------------------------------------------------------------------- /application/modules/api/Bootstrap.php: -------------------------------------------------------------------------------- 1 | registerPlugin(new REST_Controller_Plugin_RestHandler($frontController)); 10 | 11 | // add REST contextSwitch helper 12 | $contextSwitch = new REST_Controller_Action_Helper_ContextSwitch(); 13 | Zend_Controller_Action_HelperBroker::addHelper($contextSwitch); 14 | 15 | // add restContexts helper 16 | $restContexts = new REST_Controller_Action_Helper_RestContexts(); 17 | Zend_Controller_Action_HelperBroker::addHelper($restContexts); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /application/modules/api/controllers/ErrorController.php: -------------------------------------------------------------------------------- 1 | _request->hasError()) { 11 | $error = $this->_request->getError(); 12 | $this->view->message = $error->message; 13 | $this->getResponse()->setHttpResponseCode($error->code); 14 | return; 15 | } 16 | 17 | $errors = $this->_getParam('error_handler'); 18 | 19 | if (!$errors || !$errors instanceof ArrayObject) { 20 | $this->view->message = 'You have reached the error page'; 21 | return; 22 | } 23 | 24 | switch ($errors->type) { 25 | case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE: 26 | case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER: 27 | case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION: 28 | // 404 error -- controller or action not found 29 | $this->view->message = 'Page not found'; 30 | $this->getResponse()->setHttpResponseCode(404); 31 | break; 32 | 33 | default: 34 | // application error 35 | $this->view->message = 'Application error'; 36 | $this->getResponse()->setHttpResponseCode(500); 37 | break; 38 | } 39 | 40 | // conditionally display exceptions 41 | if ($this->getInvokeArg('displayExceptions') == true) { 42 | $this->view->exception = $errors->exception->getMessage(); 43 | } 44 | } 45 | 46 | /** 47 | * Catch-All 48 | * useful for custom HTTP Methods 49 | * 50 | **/ 51 | public function __callAction() 52 | { 53 | } 54 | 55 | /** 56 | * Index Action 57 | * 58 | * @return void 59 | */ 60 | public function indexAction() 61 | { 62 | } 63 | 64 | /** 65 | * GET Action 66 | * 67 | * @return void 68 | */ 69 | public function getAction() 70 | { 71 | } 72 | 73 | /** 74 | * POST Action 75 | * 76 | * @return void 77 | */ 78 | public function postAction() 79 | { 80 | } 81 | 82 | /** 83 | * PUT Action 84 | * 85 | * @return void 86 | */ 87 | public function putAction() 88 | { 89 | } 90 | 91 | /** 92 | * DELETE Action 93 | * 94 | * @return void 95 | */ 96 | public function deleteAction() 97 | { 98 | } 99 | } 100 | 101 | -------------------------------------------------------------------------------- /application/modules/api/controllers/FooController.php: -------------------------------------------------------------------------------- 1 | view->message = 'indexAction has been called.'; 14 | $this->_response->ok(); 15 | } 16 | 17 | /** 18 | * The head action handles HEAD requests; it should respond with an 19 | * identical response to the one that would correspond to a GET request, 20 | * but without the response body. 21 | */ 22 | public function headAction() 23 | { 24 | $this->view->message = 'headAction has been called'; 25 | $this->_response->ok(); 26 | } 27 | 28 | /** 29 | * The get action handles GET requests and receives an 'id' parameter; it 30 | * should respond with the server resource state of the resource identified 31 | * by the 'id' value. 32 | */ 33 | public function getAction() 34 | { 35 | $id = $this->_getParam('id', 0); 36 | 37 | $this->view->id = $id; 38 | $this->view->message = sprintf('Resource #%s', $id); 39 | $this->_response->ok(); 40 | } 41 | 42 | /** 43 | * The post action handles POST requests; it should accept and digest a 44 | * POSTed resource representation and persist the resource state. 45 | */ 46 | public function postAction() 47 | { 48 | $this->view->params = $this->_request->getParams(); 49 | $this->view->message = 'Resource Created'; 50 | $this->_response->created(); 51 | } 52 | 53 | /** 54 | * The put action handles PUT requests and receives an 'id' parameter; it 55 | * should update the server resource state of the resource identified by 56 | * the 'id' value. 57 | */ 58 | public function putAction() 59 | { 60 | $id = $this->_getParam('id', 0); 61 | 62 | $this->view->id = $id; 63 | $this->view->params = $this->_request->getParams(); 64 | $this->view->message = sprintf('Resource #%s Updated', $id); 65 | $this->_response->ok(); 66 | } 67 | 68 | /** 69 | * The delete action handles DELETE requests and receives an 'id' 70 | * parameter; it should update the server resource state of the resource 71 | * identified by the 'id' value. 72 | */ 73 | public function deleteAction() 74 | { 75 | $id = $this->_getParam('id', 0); 76 | 77 | $this->view->id = $id; 78 | $this->view->message = sprintf('Resource #%s Deleted', $id); 79 | $this->_response->ok(); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /application/modules/api/controllers/IndexController.php: -------------------------------------------------------------------------------- 1 | Api Module landing page 2 | -------------------------------------------------------------------------------- /application/modules/default/Bootstrap.php: -------------------------------------------------------------------------------- 1 | _getParam('error_handler'); 9 | 10 | if (!$errors || !$errors instanceof ArrayObject) { 11 | $this->view->message = 'You have reached the error page'; 12 | return; 13 | } 14 | 15 | switch ($errors->type) { 16 | case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE: 17 | case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER: 18 | case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION: 19 | // 404 error -- controller or action not found 20 | $this->getResponse()->setHttpResponseCode(404); 21 | $priority = Zend_Log::NOTICE; 22 | $this->view->message = 'Page not found'; 23 | break; 24 | default: 25 | // application error 26 | $this->getResponse()->setHttpResponseCode(500); 27 | $priority = Zend_Log::CRIT; 28 | $this->view->message = 'Application error'; 29 | break; 30 | } 31 | 32 | // Log exception, if logger available 33 | if ($log = $this->getLog()) { 34 | $log->log($this->view->message, $priority, $errors->exception); 35 | $log->log('Request Parameters', $priority, $errors->request->getParams()); 36 | } 37 | 38 | // conditionally display exceptions 39 | if ($this->getInvokeArg('displayExceptions') == true) { 40 | $this->view->exception = $errors->exception; 41 | } 42 | 43 | $this->view->request = $errors->request; 44 | } 45 | 46 | public function getLog() 47 | { 48 | $bootstrap = $this->getInvokeArg('bootstrap'); 49 | if (!$bootstrap->hasResource('Log')) { 50 | return false; 51 | } 52 | $log = $bootstrap->getResource('Log'); 53 | return $log; 54 | } 55 | 56 | 57 | } 58 | 59 | -------------------------------------------------------------------------------- /application/modules/default/controllers/IndexController.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Zend Framework Default Application 6 | 7 | 8 |

An error occurred

9 |

message ?>

10 | 11 | exception)): ?> 12 | 13 |

Exception information:

14 |

15 | Message: exception->getMessage() ?> 16 |

17 | 18 |

Stack trace:

19 |
exception->getTraceAsString() ?>
20 |   
21 | 22 |

Request Parameters:

23 |
escape(var_export($this->request->getParams(), true)) ?>
24 |   
25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /application/modules/default/views/scripts/index/index.phtml: -------------------------------------------------------------------------------- 1 | 30 |
31 |

Welcome to the Zend Framework!

32 | 33 |

This is your project's main page

34 | 35 |
36 |

37 |

38 | Helpful Links:
39 | Zend Framework Website | 40 | Zend Framework Manual 41 |

42 |
43 |
-------------------------------------------------------------------------------- /public/.htaccess: -------------------------------------------------------------------------------- 1 | 2 | RewriteEngine On 3 | RewriteCond %{REQUEST_FILENAME} -s [OR] 4 | RewriteCond %{REQUEST_FILENAME} -l [OR] 5 | RewriteCond %{REQUEST_FILENAME} -d 6 | RewriteRule ^.*$ - [NC,L] 7 | RewriteRule ^.*$ index.php [NC,L] 8 | -------------------------------------------------------------------------------- /public/index.php: -------------------------------------------------------------------------------- 1 | bootstrap() 26 | ->run(); 27 | --------------------------------------------------------------------------------