我知道REST API可以使用Zend_Rest_Controller实现,它有5个抽象方法indexAction,getAction,postAction,putAction,deleteAction来执行返回,创建,更新等...
我的问题是,除了这些默认函数之外,我可以在控制器中使用更多的API小说来执行各种类型的操作吗?
例如:
indexAction - 返回可用图书清单
searchAction - 根据搜索条件返回图书清单。 (我知道可以在indexAction中使用一些参数来完成,但代码看起来会更复杂,我需要避免这种情况)
答案 0 :(得分:1)
是的,您可以在控制器中创建自定义操作方法。虽然您正在扩展抽象类Zend_Rest_Controller,但只要您定义了这5个抽象方法(您已经提到过的方法),您就可以自由定制其余的类。
您可能会考虑的唯一类似方法是getAction()
。这需要一个名称为ID的参数,并将根据主键检索记录。
您可能必须在配置文件中定义路由:
routes.archive.route = "search/:keyword"
routes.archive.defaults.controller = books
routes.archive.defaults.action = search
routes.archive.defaults.year = "Hamlet"
routes.archive.reqs.year = "\s+"
然后您需要将这些配置选项提供给您的路由器:
$config = new Zend_Config_Ini('/path/to/config.ini', 'production');
$router = new Zend_Controller_Router_Rewrite();
$router->addConfig($config, 'routes');
阅读Zend's documentation以获得更深入的教程。