Yiiのシンプルで安らかな実装

はじめに


私が働いている会社のプロジェクトのために、Google Chrome拡張機能を作成することにしました。 拡張機能とプロジェクト自体は、自動トピック専用です。 書き込みの過程で、サーバー側は私を悩ませ、拡張機能に既製のデータレイアウトとスタイルを提供しましたが、jsonが必要でした。 安らぎに関する記事が目を引き、安らかなアーキテクチャでサーバー側を書くことにしました。

私たちのプロジェクトは、フレームワークを使用せずにネイティブphpで実装されています。 私は必要なフェニーを実装する単純なクラスを書きましたが、yiiのファンである私も彼のためにこれを実装することに決めたので、そこでやめられませんでした。 サードパーティの開発を見ましたが、自分が作成したクラス、さらに通常は自分で作成したクラスが、割り当てられたタスクに対処したかったのです。 yiiに適応した私のクラスは、同僚に「はい」と言って、それがどのように気に入ったかを示しました。 しかし、私は苦しみました。もっと簡単にしたかったのです。



だから


yiiには、マニュアルに書かれているように「 アクションパラメーターをバインドする」という素晴らしい機会があります-$ _GETから対応する値を自動的に取得する名前付きパラメーターを指定できます。



class UpdateAction extends CAction { public function run($id) { // $id     $_GET['id'] } }
      
      







ここで、アイデアはGETから受信するだけでなく、Prest、PUT、DELETEを受信する機会を訪れました。 その結果、runControllerメソッドでCControllerクラスを再定義し、不足しているものを追加する必要がありました。

GetActionParamsはGETの取得を担当します;いことは簡単です。

 /** * Returns the request parameters that will be used for action parameter binding. * By default, this method will return $_GET. You may override this method if you * want to use other request parameters (eg $_GET+$_POST). * @return array the request parameters to be used for action parameter binding * @since 1.1.7 */ public function getActionParams() { return $_GET; }
      
      





オーバーライドされたクラスORestControllerにメソッドを追加します。

(最新バージョン1.1.9を使用し、$ _POSTからデータを取得しました。試行しなかったため成功しませんでした。ストリームからすべてを取得する必要がありました)

 // POST  public function getActionParamsPOST() { //  $fh = fopen("php://input", 'r'); $post_string=stream_get_contents($fh); $post_param = explode("&", $post_string); $array_put=array(); foreach($post_param as $post_val) { $param = explode("=", $post_val); $array_post[$param[0]]=urldecode($param[1]); } return $array_post; } // DELETE  public function getActionParamsDELETE() { //  $fh = fopen("php://input", 'r'); $delete_string=stream_get_contents($fh); $delete_param = explode("&", $delete_string); $array_delete=array(); foreach($delete_param as $delete_val) { $param = explode("=", $delete_val); $array_delete[$param[0]]=urldecode($param[1]); } if($_GET) $_delete=$_GET; else $_delete=$array_delete; return $_delete; } // PUT  public function getActionParamsPUT() { //   PUT $fh = fopen("php://input", 'r'); $put_string=stream_get_contents($fh); $put_param = explode("&", $put_string); $array_put=array(); foreach($put_param as $put_val) { $param = explode("=", $put_val); $array_put[$param[0]]=urldecode($param[1]); } return $array_put; }
      
      







そして、元の方法をわずかに変更します

 /** * Runs the action after passing through all filters. * This method is invoked by {@link runActionWithFilters} after all possible filters have been executed * and the action starts to run. * @param CAction $action action to run * *  runAction   PUT,DELETE,POST */ public function runAction($action) { $priorAction=$this->_action; $this->_action=$action; $params=false; if($this->beforeAction($action)) { switch ($_SERVER['REQUEST_METHOD']) { case "POST": $params= $this->getActionParamsPOST(); break; case "PUT": $params= $this->getActionParamsPUT(); break; case "DELETE": $params= $this->getActionParamsDELETE(); break; default: $params= $this->getActionParams(); } if($action->runWithParams($params)===false) $this->invalidActionParams($action); else $this->afterAction($action); } $this->_action=$priorAction; }
      
      







さて、それがすべての原則です。新しいコントローラーと作業を継承します。

例:

 class ApiController extends ORestController { public function actions() { return array ( 'test' => 'application.controllers.actionsApi.actionTest', ); } }
      
      







 class actionTest extends CAction { public function run($params='') { switch ($_SERVER['REQUEST_METHOD']) { case "POST": echo "POST ".$params; break; case "PUT": echo "PUT ".$params; break; case "DELETE": echo "DELETE ".$params; break; default: echo "GET ".$params; } } }
      
      





各アクションに必要な機能を実装することは残っており、各アクションはrestfulで説明されているように個別のエンティティで動作します。 したがって、URLルールを設定する必要はありませんが、アプリケーションのパフォーマンスには影響しません。

PS:getActionParamsPOST、getActionParamsPUT、getActionParamsDELETEの各メソッドを1つのメソッドに減らすことで、別のコードを減らすことができます。



All Articles