Saturday 14 July 2007, 13:33
A Zend controller plugin to enable RESTful behaviour
By Geoffrey - Coding - Permalink
This is a simple controller plugin for the Zend Framework which enable RESTful behaviour. It basically adds the HTTP method name to the action name, so that the URL http://example.com/foo/bar will be dispatched to FooController::barGetAction on a GET, FooController::barPostAction on a POST, etc.
Here is the actual code:
<?php
class My_Controller_Plugin_Rest extends Zend_Controller_Plugin_Abstract {
/**
* Defines the format of the REST action name
* Quite useless atm as the dispatcher will strip
* any non alpha character
*/
protected $_nameFormat = ':action:method';
/**
* Rewrites the action according to the http method
*/
public function preDispatch() {
$request = $this->getRequest();
$restActionName = $this->_translateSpec($this->_nameFormat, array(
'action' => $request->getActionName(),
'method' => $request->getMethod(),
));
$request->setActionName($restActionName);
}
/**
* Inject values into a spec strings
*
* Allowed values are:
* :action => the action name
* :method => the http method
*
* @param string $spec
* @param array $vars
* @return string
*/
protected function _translateSpec($spec, $vars = array()) {
foreach($vars as $key => $value) {
switch($key) {
case 'action':
case 'method':
$$key = $value;
break;
default:
break;
}
}
$replacements = array(
':action' => $action,
':method' => $method,
);
$value = str_replace(array_keys($replacements), array_values($replacements),$spec);
return $value;
}
}
Still, i'm not completly satisfied with this plugin. Plugins certainly allows for powerful control over what's going up in the dispatch process, but the dispatcher itself enforces a set of rules on actions naming (eg, you can't have a _ in it, it is stripped at dispatch time). Thus, I'm wondering on the pertinence of writting a custom dispatcher (read My_Controller_Dispatcher_Rest) instead of just a plugin, which would enable far more possibilities.
Btw, in case you're wondering, the plugins is used like this;
$frontController = Zend_Controller_Front::getInstance(); $frontController->registerPlugin(new My_Controller_Plugin_Rest);
Easy heh ?
Also, I'm not convinced that this plugin is the way to go in matter of RESTful functionnality. I'm still wondering if it would not be better to have urls mapped to a single controller, replacing actions with http methods (that is, http://example.com/foo/bar would map to FooController::getAction, etc).
Any opinions around ?
4 comments
Pas vraiment en rapport avec l'article, mais c'est juste pour dire que le formulaire pour poster un commentaire serait plus sympa avec ça dans la feuille de style :
#comment-form label { float: left; width: 130px; }Voilà :-)
comme ça par exemple ? :)
Comme ça aussi c'est sympa oui :-)
J'avais proposé de mettre width:130px parce que ça mettait les labels devant les fields avec les fields bien alignés, mais avec width:50% c'est sympa aussi d'avoir les fields à la ligne :-)
hum chez moi ils sont alignés a droite, t'es sous firefox ou bien ?
This post's comments feed