不允许使用 405 方法 - CodeIgniter Rest-server
405 method not allowed - CodeIgniter Rest-server
我在使用 Codeigniter - Rest Server 方面已经有一周的问题了。
我有一个名为 Users 的控制器,它有两种方法 all_users_get 和 register_post .
- all_users_get 工作正常。
register_post
returns { "status": false, "error": "Unknown method" }
如果我为 POST 更改方法 all_users_get 我会得到同样的错误,只适用于 GET
<?php
use Restserver\Libraries\REST_Controller;
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';
class Users extends REST_Controller {
function __construct()
{
// Construct the parent class
parent::__construct();
$this->load->model('user_model');
// Configure limits on our controller methods
// Ensure you have created the 'limits' table and enabled 'limits' within application/config/rest.php
$this->methods['users_get']['limit'] = 500; // 500 requests per hour per user/key
$this->methods['users_post']['limit'] = 100; // 100 requests per hour per user/key
$this->methods['users_delete']['limit'] = 50; // 50 requests per hour per user/key
}
/**
* @method: GET
* Fetch all users
*/
public function all_users_get()
{
$users = $this->user_model->all_users();
// Check if the users data store contains users (in case the database result returns NULL)
if ($users)
{
// Set the response and exit
$this->response($users, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
}
else
{
// Set the response and exit
$this->response([
'status' => FALSE,
'message' => 'No users were found'
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
}
$this->response($users, REST_Controller::HTTP_OK);
}
/**
* User Register
* *************************
*
* @param: fullname
* @param: email address
* @param: password
* @param: username
*
* *************************
* @method: POST
* @link : api/users/register
*/
public function resgister_post()
{
header('Access-Control-Allow-Origin: *');
$this->response("Teste", REST_Controller::HTTP_OK);
}
}
我是 运行 我在本地主机的代码 Xampp 我在 phpinfo
上找到了
_SERVER["REQUEST_METHOD"] GET
我很困惑我不知道这是否与 Xampp conf 或我的代码有关,请帮忙。谢谢大家
This function should work for you :)
public function index_post()// post data to the database
{
$data = [
'first_name' => $this->post('first_name'),
'last_name' => $this->post('last_name'),
'email' => $this->post('email')
];
if( $this->users->createUser($data) > 0)
{
$this->response([
'status' => true,
'message' => 'NEW USER CREATED'
], REST_Controller::HTTP_CREATED);
}
else{
$this->response([
'status' => false,
'message' => 'FAILED TO CREATE NEW USER'
], REST_Controller::HTTP_BAD_REQUEST);
}
}
header('Access-Control-Allow-Origin: *');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
exit;
}
只需在对我有用的构造函数中复制上面的行
最终解决方案
第 1 步
在您的构造函数中,添加以下代码
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
$method = $_SERVER['REQUEST_METHOD'];
if ($method == "OPTIONS") {
die();
}
第 2 步:
打开你的 route.php 并定义一条 API 路线
例如
$route['api/v1/auth/login'] = 'api/v1/auth/login';
注意:该路线将在第3步中使用
第 3 步:
打开你的config.php,添加下面的代码
if (stripos($_SERVER["REQUEST_URI"],'/api/v1/auth/login/') === FALSE) {
$config['csrf_protection'] = FALSE;
}else{
$config['csrf_protection'] = FALSE;
}
注意:将/api/v1/auth/login/
更改为您的端点
我在使用 Codeigniter - Rest Server 方面已经有一周的问题了。 我有一个名为 Users 的控制器,它有两种方法 all_users_get 和 register_post .
- all_users_get 工作正常。
register_post
returns { "status": false, "error": "Unknown method" }
如果我为 POST 更改方法 all_users_get 我会得到同样的错误,只适用于 GET
<?php
use Restserver\Libraries\REST_Controller;
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';
class Users extends REST_Controller {
function __construct()
{
// Construct the parent class
parent::__construct();
$this->load->model('user_model');
// Configure limits on our controller methods
// Ensure you have created the 'limits' table and enabled 'limits' within application/config/rest.php
$this->methods['users_get']['limit'] = 500; // 500 requests per hour per user/key
$this->methods['users_post']['limit'] = 100; // 100 requests per hour per user/key
$this->methods['users_delete']['limit'] = 50; // 50 requests per hour per user/key
}
/**
* @method: GET
* Fetch all users
*/
public function all_users_get()
{
$users = $this->user_model->all_users();
// Check if the users data store contains users (in case the database result returns NULL)
if ($users)
{
// Set the response and exit
$this->response($users, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
}
else
{
// Set the response and exit
$this->response([
'status' => FALSE,
'message' => 'No users were found'
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
}
$this->response($users, REST_Controller::HTTP_OK);
}
/**
* User Register
* *************************
*
* @param: fullname
* @param: email address
* @param: password
* @param: username
*
* *************************
* @method: POST
* @link : api/users/register
*/
public function resgister_post()
{
header('Access-Control-Allow-Origin: *');
$this->response("Teste", REST_Controller::HTTP_OK);
}
}
我是 运行 我在本地主机的代码 Xampp 我在 phpinfo
上找到了_SERVER["REQUEST_METHOD"] GET
我很困惑我不知道这是否与 Xampp conf 或我的代码有关,请帮忙。谢谢大家
This function should work for you :)
public function index_post()// post data to the database
{
$data = [
'first_name' => $this->post('first_name'),
'last_name' => $this->post('last_name'),
'email' => $this->post('email')
];
if( $this->users->createUser($data) > 0)
{
$this->response([
'status' => true,
'message' => 'NEW USER CREATED'
], REST_Controller::HTTP_CREATED);
}
else{
$this->response([
'status' => false,
'message' => 'FAILED TO CREATE NEW USER'
], REST_Controller::HTTP_BAD_REQUEST);
}
}
header('Access-Control-Allow-Origin: *');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
exit;
}
只需在对我有用的构造函数中复制上面的行
最终解决方案 第 1 步 在您的构造函数中,添加以下代码
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
$method = $_SERVER['REQUEST_METHOD'];
if ($method == "OPTIONS") {
die();
}
第 2 步: 打开你的 route.php 并定义一条 API 路线 例如
$route['api/v1/auth/login'] = 'api/v1/auth/login';
注意:该路线将在第3步中使用
第 3 步: 打开你的config.php,添加下面的代码
if (stripos($_SERVER["REQUEST_URI"],'/api/v1/auth/login/') === FALSE) {
$config['csrf_protection'] = FALSE;
}else{
$config['csrf_protection'] = FALSE;
}
注意:将/api/v1/auth/login/
更改为您的端点