문제

Here is the code in my unit test...

public static function member_put($f3,$args) {
    // Id is for member: locohost
    $f3->mock('PUT /member/c4774904-f15f-11e2-b7e4-00ffe024bd0b', array(
        'firstname' => 'Not-Mark', 
        'lastname' => 'Not-Deibert' 
    )); 
}

Here is the Member model put method being called...

public static function put($f3,$args) {
    self::validateArgs($args);
    self::validatePost();
    self::findById($args['id']);
    self::$member->copyFrom('POST');
    //var_dump(self::$member);
    self::$member->save();
    self::returnModel();
}

The Member put method is being called as expected, however the Member is not getting the new name fields from copyFrom('POST'). The var_dump still shows old values in the name fields. What am I doing wrong?

도움이 되었습니까?

해결책

$_POST is available for POST method only. For other methods (PUT, PATCH, DELETE) but also POST and GET, the http request body is stored in the BODY variable.

Therefore your put() function should look like:

public static function put($f3,$args) {
    self::validateArgs($args);
    self::validatePost();
    self::findById($args['id']);
    parse_str($f3->get('BODY'),$input);
    $f3->set('INPUT',$input);
    self::$member->copyFrom('INPUT');
    //var_dump(self::$member);
    self::$member->save();
    self::returnModel();
}

Note that the request body is in the form of a query string: firstname=Not-Mark&lastname=Not-Deibert. That explains why it needs to be parsed with parse_str.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top