Phil Sturgeon的REST服务器,Codeigniter3,错误消息在PUT上没有返回

spr*_*man 6 php rest codeigniter codeigniter-restserver

我正在使用Phil Sturgeon的REST服务器,CI3和POSTMAN进行调试.我发送了一个PUT以下信息,但是,我没有收到预期的错误消息.

这是我的form_validation.php:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config = array(
  'student_put' => array(
    array('field' => 'email_address', 'label' => 'email_address', 'rules' => 'trim|required|valid_email'),
    array('field' => 'password', 'label' => 'password', 'rules' => 'trim|required|min_length[8]|max_length[16]'),
    array('field' => 'first_name', 'label' => 'first_name', 'rules' => 'trim|required|max_length[50]'),
    array('field' => 'last_name', 'label' => 'last_name', 'rules' => 'trim|required|max_length[50]'),
    array('field' => 'phone_number', 'label' => 'phone_number', 'rules' => 'trim|required|alpha_dash'),
  )
);

?>
Run Code Online (Sandbox Code Playgroud)

这是我的控制器Api.php中的方法:

function student_put(){
    $this->form_validation->set_data($this->put());
    // these are the rules set in config/form_validation.php
    if ($this->form_validation->run('student_put') != FALSE) {
        die('good data');
    } else { 
        $this->response( 
            array(
                'status'=> 'failure', 
                'message'=> $this->form_validation->get_errors_as_array(),
                ), 
            REST_Controller::HTTP_BAD_REQUEST  
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的库文件夹中的MY_Form_validation.php:

<?php

class MY_Form_validation extends CI_Form_validation {

  function __construct($rules = array()) {
      parent::__construct($rules);
      $this->ci =& get_instance();
  }

  public function get_errors_as_array() {
      return $this->_error_array;
  }

  public function get_config_rules() {
      return $this->_config_rules;
  }

  public function get_field_names($form) {
      $field_names = array();
      $rules = $this->get_config_rules();
      $rules = $rules[$form];
      foreach ($rules as $index=> $info) {
          $field_names[] = $info['field'];
      }
      return $field_names;
  }
}
Run Code Online (Sandbox Code Playgroud)

当我在POSTMAN中放下以下内容时:

X-API-KEY          123456
first_name         test
email_address      abc
Run Code Online (Sandbox Code Playgroud)

这是我得到的结果:

{
  "status": "failure",
  "message": []
}
Run Code Online (Sandbox Code Playgroud)

但我应该得到验证错误.

作为调试步骤,我已经确认: - 没有auth错误 - 正在读取form_validation.php - 如果我改变:

'message'=> $this->form_validation->get_errors_as_array(),
Run Code Online (Sandbox Code Playgroud)

'message'=> 'test',
Run Code Online (Sandbox Code Playgroud)

邮递员回复:

{
"status": "failure",
"message": "test"
}
Run Code Online (Sandbox Code Playgroud)

任何帮助非常感谢.

小智 3

你必须阅读这个链接,

http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter-2--net-8814

如果使用apikey,则必须设置

    $config['rest_auth'] = 'basic'
    $config['rest_enable_keys'] = TRUE;
Run Code Online (Sandbox Code Playgroud)

还在数据库中创建一个表用于存储 api 密钥

CREATE TABLE `keys` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `user_id` INT(11) NOT NULL,
    `key` VARCHAR(40) NOT NULL,
    `level` INT(2) NOT NULL,
    `ignore_limits` TINYINT(1) NOT NULL DEFAULT '0',
    `is_private_key` TINYINT(1)  NOT NULL DEFAULT '0',
    `ip_addresses` TEXT NULL DEFAULT NULL,
    `date_created` INT(11) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Run Code Online (Sandbox Code Playgroud)

插入该数据库至少 1 行,重要的列只有键,它是 apikey

出于安全原因,apikey 必须包含 40 位字母数字

再说一次,你必须阅读文档,以及 application/config 中的rest.php

    $config['rest_valid_logins'] = ['admin' => '1234'];
Run Code Online (Sandbox Code Playgroud)

该登录名是默认设置的,因此您必须将该登录名插入到客户端请求的标头等中

    http_user           'admin'
    http_pass           '1234'
    X-API-KEY           '123456'
    first_name          test
    email_address       abc
Run Code Online (Sandbox Code Playgroud)

如果该标头不起作用,请尝试此操作

    http_user           'admin'
    http_pass           '1234'
    api_name            'X-API-KEY'
    api_key             '123456'
    first_name          test
    email_address       abc
Run Code Online (Sandbox Code Playgroud)

如果您之前尝试过这样的请求

    $config['rest_auth'] = FALSE
Run Code Online (Sandbox Code Playgroud)

实际上你还没有保护你的 api web 服务