Yii Framework Forum: How To Get Json Decoded Post/delete Data - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Get Json Decoded Post/delete Data Rate Topic: -----

#1 User is offline   radzserg 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 13-June 12

Posted 29 January 2013 - 09:54 AM

Hi guys, I have a problem with getRestParams method

Look at CHttpRequest
        public function getRestParams()
	{
		if($this->_restParams===null)
		{
			$result=array();
			if(function_exists('mb_parse_str'))
				mb_parse_str($this->getRawBody(), $result);
			else
				parse_str($this->getRawBody(), $result);
			$this->_restParams=$result;
		}

		return $this->_restParams;
	}


But what about JSON encoded data. For example Backbone JS likes to send JSON encoded data. Of course I can change dataType on client side but the issue still exists. So I assume to get such method

        public function getRestParams()
	{
	    if($this->_restParams===null) {
		$result=array();
                if ($_SERVER['CONTENT_TYPE'] == 'application/json') {
                    $result = \CJSON::decode($this->getRawBody());
                } else {
                    if(function_exists('mb_parse_str')) {
                        mb_parse_str($this->getRawBody(), $result);
                    } else {
                        parse_str($this->getRawBody(), $result);
                    }
                }
	        $this->_restParams=$result;
	    }
	    return $this->_restParams;
        }


And again I could override CHttpRequest but $_restParams is private. For now I have to override also getDelete and getPut methods in order to make it works.

What you think about this issue.
0

#2 User is offline   radzserg 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 13-June 12

Posted 02 February 2013 - 04:21 AM

Added the full code of my custom HttpRequest that handle JSON decoded data here
radzserg.com/2013/02/01/set-up-yii-and-backbone-js/
And some tips in order to use yii with backbone.

Hope this will help somebody.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users