Skip to content

Instantly share code, notes, and snippets.

@claussni
Created February 21, 2010 13:33
Show Gist options
  • Save claussni/310313 to your computer and use it in GitHub Desktop.
Save claussni/310313 to your computer and use it in GitHub Desktop.
Zend_Rest_Route in Action
<?php
$methods = array(
'GET',
'PUT',
'POST',
'DELETE'
);
foreach ($methods as $method) {
$url = 'http://helloworldapp/snafu';
echo "$method: $url\n";
// fool front controller to avoid complex application setup
$front = Zend_Controller_Front::getInstance();
$front->resetInstance();
$front->throwExceptions(true);
$front->setParam('noViewRenderer', true);
$front->setControllerDirectory('.');
// add REST routing
$router = $front->getRouter();
$route = new Zend_Rest_Route($front);
$router->addRoute('rest', $route);
// simulate HTTP method
$_SERVER['REQUEST_METHOD'] = $method;
// construct Zend Request object
$request = new Zend_Controller_Request_Http($url);
// dispatch Request
Zend_Controller_Front::getInstance()->dispatch($request);
}
echo "\n";
GET: http://helloworldapp/snafu
Array
(
[controller] => snafu
[action] => index
[module] => default
)
PUT: http://helloworldapp/snafu
Array
(
[controller] => snafu
[action] => put
[module] => default
)
POST: http://helloworldapp/snafu
Array
(
[controller] => snafu
[action] => post
[module] => default
)
DELETE: http://helloworldapp/snafu
Array
(
[controller] => snafu
[action] => delete
[module] => default
)
<?php
class SnafuController
extends Zend_Rest_Controller {
public function indexAction() {
// Zend Dispatcher routes to indexAction on every get request
// so pass over to getAction
$this->getAction();
}
public function getAction() {
print_r($this->getRequest()->getParams());
}
public function putAction() {
print_r($this->getRequest()->getParams());
}
public function postAction() {
print_r($this->getRequest()->getParams());
}
public function deleteAction() {
print_r($this->getRequest()->getParams());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment