【问题标题】:Custom Error Messages to CodeIgniter Rest APICodeIgniter Rest API 的自定义错误消息
【发布时间】:2016-06-28 16:45:02
【问题描述】:

您好,我正在使用 https://github.com/chriskacerguis/codeigniter-restserver/blob/master/application/libraries/REST_Controller.php 使用 CodeIgniter 构建 REST API。

在 _detect_api_key() 函数中,他只检查给定的 Api Key 是否与存储在表中的匹配。我对这个方法进行了一些修改,不仅检查 api 密钥是否匹配,还检查用户是否被激活。对于这两种情况,我都会收到 Invalid API Key 消息。

如何将消息设置为“您使用的 API 密钥未激活”或类似内容?

我用于错误消息的文件是这样的:

/*
 * English language
 */

$lang['text_rest_invalid_api_key'] = 'Invalid API key %s'; // %s is the REST API key
$lang['text_rest_invalid_credentials'] = 'Invalid credentials';
$lang['text_rest_ip_denied'] = 'IP denied';
$lang['text_rest_ip_unauthorized'] = 'IP unauthorized';
$lang['text_rest_unauthorized'] = 'Unauthorized';
$lang['text_rest_ajax_only'] = 'Only AJAX requests are allowed';
$lang['text_rest_api_key_unauthorized'] = 'This API key does not have access to the requested controller';
$lang['text_rest_api_key_permissions'] = 'This API key does not have enough permissions';
$lang['text_rest_api_key_time_limit'] = 'This API key has reached the time limit for this method';
$lang['text_rest_unknown_method'] = 'Unknown method';
$lang['text_rest_unsupported'] = 'Unsupported protocol';

并且在 _detect_api_key 方法中没有提及任何有关错误消息的内容,只是返回 false:

 if ( !($row = $this->rest->db->where($this->config->item('rest_key_column'), $key)->get($this->config->item('rest_keys_table'))->row()) ||   $isactive[0]->activated === 'no' )
            {
                return FALSE;
            }

我添加了这个:

$isactive[0]->activated === 'no'

有什么想法吗?

【问题讨论】:

    标签: php codeigniter api rest


    【解决方案1】:

    试试这个

    if (!($row = $this->rest->db
    ->where($this->config->item('rest_key_column'), $key)
    ->get($this->config->item('rest_keys_table'))->row())) 
    {
        return array(
            "valid_key" => FALSE, 
            "error" => "text_rest_invalid_api_key"
        );
    } else if($isactive[0]->activated === 'no') {
        return array(
            "valid_key" => FALSE, 
            "error" => "user_not_activated"
        );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-14
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多