Created
August 25, 2011 08:57
-
-
Save bytespider/1170276 to your computer and use it in GitHub Desktop.
Zend Framework Rest Route for "/{api_version}/{controller}/{identifier}/{action}" uri scheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap | |
{ | |
protected function _initRouter() | |
{ | |
$this->bootstrap('frontcontroller'); | |
$frontController = $this->getResource('frontController'); | |
$restRoute = new M5_Rest_Route($frontController, array('module'=>'api'), array('api')); | |
$router->addRoute('api', $restRoute); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class App_Rest_Route extends Zend_Rest_Route | |
{ | |
public function match($request, $partial = false) | |
{ | |
if (!$request instanceof Zend_Controller_Request_Http) { | |
$request = $this->_front->getRequest(); | |
} | |
$this->_request = $request; | |
$this->_setRequestKeys(); | |
$path = $request->getPathInfo(); | |
$params = $request->getParams(); | |
$values = array(); | |
$path = trim($path, self::URI_DELIMITER); | |
if ($path != '') { | |
$path = explode(self::URI_DELIMITER, $path); | |
$params['api_version'] = array_shift($path); | |
if (!is_numeric($params['api_version'])) { | |
// not a version number so not a rest route | |
return false; | |
} | |
// Determine Module | |
$moduleName = $this->_defaults[$this->_moduleKey]; | |
$dispatcher = $this->_front->getDispatcher(); | |
if ($dispatcher && $dispatcher->isValidModule($path[0])) { | |
$moduleName = $path[0]; | |
if ($this->_checkRestfulModule($moduleName)) { | |
$values[$this->_moduleKey] = array_shift($path); | |
$this->_moduleValid = true; | |
} | |
} | |
// Determine Controller | |
$controllerName = $this->_defaults[$this->_controllerKey]; | |
if (count($path) && !empty($path[0])) { | |
if ($this->_checkRestfulController($moduleName, $path[0])) { | |
$controllerName = $path[0]; | |
$values[$this->_controllerKey] = array_shift($path); | |
$values[$this->_actionKey] = 'get'; | |
} else { | |
// If Controller in URI is not found to be a RESTful | |
// Controller, return false to fall back to other routes | |
return false; | |
} | |
} elseif ($this->_checkRestfulController($moduleName, $controllerName)) { | |
$values[$this->_controllerKey] = $controllerName; | |
$values[$this->_actionKey] = 'get'; | |
} else { | |
return false; | |
} | |
//Store path count for method mapping | |
$pathElementCount = count($path); | |
// Check for "special get" URI's | |
$specialGetTarget = false; | |
if ($pathElementCount && array_search($path[0], array('index', 'new')) > -1) { | |
$specialGetTarget = array_shift($path); | |
} elseif ($pathElementCount && $path[$pathElementCount-1] == 'edit') { | |
$specialGetTarget = 'edit'; | |
$params['id'] = urldecode($path[$pathElementCount-2]); | |
} elseif ($pathElementCount == 1) { | |
$params['id'] = urldecode(array_shift($path)); | |
} elseif ($pathElementCount == 0 && !isset($params['id'])) { | |
$specialGetTarget = 'index'; | |
} elseif ($pathElementCount) { | |
$params['id'] = urldecode(array_shift($path)); | |
$specialGetTarget = urldecode(array_shift($path)); | |
} | |
// Digest URI params | |
if ($numSegs = count($path)) { | |
for ($i = 0; $i < $numSegs; $i = $i + 2) { | |
$key = urldecode($path[$i]); | |
$val = isset($path[$i + 1]) ? $path[$i + 1] : null; | |
$params[$key] = urldecode($val); | |
} | |
} | |
// Determine Action | |
$requestMethod = strtolower($request->getMethod()); | |
if ($requestMethod != 'get') { | |
if ($request->getParam('_method')) { | |
$values[$this->_actionKey] = strtolower($request->getParam('_method')); | |
} elseif ( $request->getHeader('X-HTTP-Method-Override') ) { | |
$values[$this->_actionKey] = strtolower($request->getHeader('X-HTTP-Method-Override')); | |
} else { | |
$values[$this->_actionKey] = $requestMethod; | |
} | |
// Map PUT and POST to actual create/update actions | |
// based on parameter count (posting to resource or collection) | |
switch( $values[$this->_actionKey] ){ | |
case 'post': | |
if ($pathElementCount > 0) { | |
$values[$this->_actionKey] = 'put'; | |
} else { | |
$values[$this->_actionKey] = 'post'; | |
} | |
break; | |
case 'put': | |
$values[$this->_actionKey] = 'put'; | |
break; | |
} | |
} elseif ($specialGetTarget) { | |
$values[$this->_actionKey] = $specialGetTarget; | |
} | |
} | |
$this->_values = $values + $params; | |
$result = $this->_values + $this->_defaults; | |
if ($partial && $result) | |
$this->setMatchedPath($request->getPathInfo()); | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When using put the class in /library/App/Rest/Route.php