1 回答

TA貢獻1836條經驗 獲得超5個贊
這是一個 ZF1 應用程序樹,ZF1 有其 REST 實現。
假設您將其稱為“MyRestfulController”。
那么你必須注冊你的休息路線,你可以在你的 Bootstrap.php 中完成
protected function _initRestRoute()
{
? ? $frontController = $this->bootstrap('frontController')->getResource("frontController");
? ? $restRouteUL = new Zend_Rest_Route($frontController, array(), [
? ? ? ? 'default' => [
? ? ? ? ? ? 'my-restful'
? ? ? ? ]
? ? ]);
? ? $frontController->getRouter()->addRoute('rest', $restRouteUL);
}
或者
如果您不需要休息,而只是返回一些 JSON 的 API,您可以跳過 Restful 部分并禁用控制器中的布局(因此不擴展 Zend_Rest_Controller ),覆蓋“init()”方法
? ? public function init()
{
? ? parent::init();
? ? $this->_helper->layout->disableLayout();
? ? $this->_helper->viewRenderer->setNoRender();
? ? $this->getResponse()->setHeader("Content-type", "text/json");
? ??
? ? /**
? ? ?* This is important for the helper not to exit the dispatch loop
? ? ?*/
? ? $this->_helper->json->suppressExit = true;
}
那么在你的行動中
public function getMyDataAction()
{
? ? $data = [];
? ? // your filters and all the logic to populate $data
? ? $this->_helper->json($data);
}
請記住,ZF1 有幾個性能問題,主要與資源配置有關,應盡可能用 serviceManager 替換,也可以用 Zend_Date 替換。
- 1 回答
- 0 關注
- 132 瀏覽
添加回答
舉報