亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

針對在 ZF 上創建的網站的簡單 REST API 實現

針對在 ZF 上創建的網站的簡單 REST API 實現

PHP
慕沐林林 2023-10-21 17:18:51
我有一個存儲庫存的網站,我需要創建一個 REST API,因為我必須放置 Web 組件并向其傳遞數據。通信將由智威湯遜 (JWT) 保障。我找到了一個非常簡單的解決方案zf3-rest-api但我無法實現它,因為我有一些奇怪的文件結構(我沒有modules.config.php等)我擔心它不是 ZF3 甚至不是 ZF2。我可以編寫自定義解決方案,但我不知道應該將代碼放在哪里(抱歉我是前端開發人員)?在模塊中?以及如何處理路由?這樣我就可以通過http://example.com/api/?來參考它
查看完整描述

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 替換。


查看完整回答
反對 回復 2023-10-21
  • 1 回答
  • 0 關注
  • 132 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號