目前我有一个用于事件控制器(/ event)的REST路由。我想在EventSignUp控制器中处理事件SignUps,并将此控制器映射到/ event / signups路由。
Zend Framework documentation表示URL / event / signup /:id应映射到Event_SignupController。但这对我不起作用。
我为Bootstrap类中的所有控制器设置了默认的REST路由:
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
// Specifying all controllers as RESTful:
$restRoute = new Zend_Rest_Route($front);
$router->addRoute('default', $restRoute);
我错过了什么或文档错了吗?如果文档错误,我应该采取什么方法来实现我的目标?
作为旁注,很多现有的控制器都依赖于默认的REST路由,所以如果有一个解决方案不需要为所有现有控制器实现新路由,那就太好了。
击>
编辑:文档说明/ product / ratings将被转换为Product_RatingsController,这意味着Products模块中的RatingsController。因为我的所有控制器都放在默认模块中,所以Rest Route不支持我想要的行为。
所以这改变了我的问题,是否有可能在不影响现有控制器对默认Rest路由的依赖性的情况下实现我想要的行为?如果是这样,怎么样?如果没有,对我来说最好的方法是什么?
答案 0 :(得分:1)
根据Haim Evgi的评论,我创建了一个控制器插件,它根据请求方法添加了Zend_Controller_Router_Route路由。这是该控制器插件的代码:
class TW_Webservice_Controller_Plugin_RestRoutes extends Zend_Controller_Plugin_Abstract
{
/**
*
* @var Zend_Controller_Router_Interface
*/
public $router;
/**
* Setup Rest routes that are not handled by the default Zend_Rest_Route object.
*
* @param Zend_Controller_Request_Abstract $request
*/
public function routeStartup(Zend_Controller_Request_Abstract $request)
{
$front = Zend_Controller_Front::getInstance();
$this->router = $front->getRouter();
$method = strtolower($request->getMethod());
$restRoutes = array(
'/event/signup' => 'event-signup'
);
$this->addRoutes($method, $restRoutes);
}
/**
*
* @param string $method The request method
* @param array $restRoutes Router pattern => Controller name pairs
*/
public function addRoutes($method, $restRoutes)
{
foreach ($restRoutes as $routePattern => $controllerName) {
switch ($method) {
case "get":
$this->addGetRoutes($routePattern, $controllerName);
break;
case "post":
$this->addPostRoute($routePattern, $controllerName);
break;
case "put":
$this->addPutRoute($routePattern, $controllerName);
break;
case "delete";
$this->addDeleteRoute($routePattern, $controllerName);
break;
}
}
}
/**
*
* @param string $routePattern
* @param string $controllerName
*/
public function addGetRoutes($routePattern, $controllerName)
{
$this->addRestRoute($routePattern, $controllerName, 'index');
$routePattern = $routePattern . '/:id';
$this->addRestRoute($routePattern, $controllerName, 'get');
}
/**
*
* @param string $routePattern
* @param string $controllerName
*/
public function addPostRoute($routePattern, $controllerName)
{
$this->addRestRoute($routePattern, $controllerName, 'post');
}
/**
*
* @param string $routePattern
* @param string $controllerName
*/
public function addPutRoute($routePattern, $controllerName)
{
$routePattern = $routePattern . '/:id';
$this->addRestRoute($routePattern, $controllerName, 'put');
}
/**
*
* @param string $routePattern
* @param string $controllerName
*/
public function addDeleteRoute($routePattern, $controllerName)
{
$routePattern = $routePattern . '/:id';
$this->addRestRoute($routePattern, $controllerName, 'delete');
}
/**
*
* @param string $routePattern
* @param string $controllerName
* @param string $action
*/
public function addRestRoute($routePattern, $controllerName, $action)
{
$route = new Zend_Controller_Router_Route($routePattern, array(
'controller' => $controllerName,
'action' => $action
));
$this->router->addRoute($controllerName . '-' . $action, $route);
}
}
如果从配置文件中检索$ restRoutes数组会更好,但现在可行。