class UpdateAction extends CAction { public function run($id) { // $id $_GET['id'] } }
/** * 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; }
// 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; } } }
Source: https://habr.com/ru/post/135862/
All Articles