Я следовал этому учебнику онлайн, и в настоящее время я использую High Sierra MacOS. Я попробовал простой restAPI с использованием PHP, и теперь я пытаюсь реализовать его на CodeIgniter3.x
фреймворке. Тот, который я создал, может успешно POST и GET на POSTMAN
, но теперь, когда я попытался следовать руководству, у меня была эта ошибка на POSTMAN
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>Not Found</h1>
<p>The requested URL /~paul/CodeIgniter/api/example/user/ was not found on this server.</p>
</body>
</html>
URL-адрес, который я использую в прошлый раз, когда я создал свой собственный API, выглядит следующим образом
http://localhost/~paul/v1/login.php
Я думаю, что есть проблема с настройкой моего CodeIgniter на High Sierra MacOS?
Может кто-нибудь, пожалуйста, помогите мне. Спасибо
РЕДАКТИРОВАТЬ: Если информации недостаточно, пожалуйста, дайте мне знать.
РЕДАКТИРОВАТЬ: я пытался проблема чувствительности к регистру
РЕДАКТИРОВАТЬ: Добавлена информация
Вот мой .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?x=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php/$1 [QSA,NC,L]
и мой api.php
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
//include Rest Controller library
require APPPATH . '/libraries/REST_Controller.php';
class Example extends REST_Controller {
public function __construct() {
parent::__construct();
//load user model
$this->load->model('user');
}
public function user_get($id = 0) {
//returns all rows if the id parameter doesn't exist,
//otherwise single row will be returned
$users = $this->user->getRows($id);
//check if the user data exists
if(!empty($users)){
//set the response and exit
$this->response($users, REST_Controller::HTTP_OK);
}else{
//set the response and exit
$this->response([
'status' => FALSE,
'message' => 'No user were found.'
], REST_Controller::HTTP_NOT_FOUND);
}
}
public function user_post() {
$userData = array();
$userData['first_name'] = $this->post('first_name');
$userData['last_name'] = $this->post('last_name');
$userData['email'] = $this->post('email');
$userData['phone'] = $this->post('phone');
if(!empty($userData['first_name']) && !empty($userData['last_name']) && !empty($userData['email']) && !empty($userData['phone'])){
//insert user data
$insert = $this->user->insert($userData);
//check if the user data inserted
if($insert){
//set the response and exit
$this->response([
'status' => TRUE,
'message' => 'User has been added successfully.'
], REST_Controller::HTTP_OK);
}else{
//set the response and exit
$this->response("Some problems occurred, please try again.", REST_Controller::HTTP_BAD_REQUEST);
}
}else{
//set the response and exit
$this->response("Provide complete user information to create.", REST_Controller::HTTP_BAD_REQUEST);
}
}
public function user_put() {
$userData = array();
$id = $this->put('id');
$userData['first_name'] = $this->put('first_name');
$userData['last_name'] = $this->put('last_name');
$userData['email'] = $this->put('email');
$userData['phone'] = $this->put('phone');
if(!empty($id) && !empty($userData['first_name']) && !empty($userData['last_name']) && !empty($userData['email']) && !empty($userData['phone'])){
//update user data
$update = $this->user->update($userData, $id);
//check if the user data updated
if($update){
//set the response and exit
$this->response([
'status' => TRUE,
'message' => 'User has been updated successfully.'
], REST_Controller::HTTP_OK);
}else{
//set the response and exit
$this->response("Some problems occurred, please try again.", REST_Controller::HTTP_BAD_REQUEST);
}
}else{
//set the response and exit
$this->response("Provide complete user information to update.", REST_Controller::HTTP_BAD_REQUEST);
}
}
public function user_delete($id){
//check whether post id is not empty
if($id){
//delete post
$delete = $this->user->delete($id);
if($delete){
//set the response and exit
$this->response([
'status' => TRUE,
'message' => 'User has been removed successfully.'
], REST_Controller::HTTP_OK);
}else{
//set the response and exit
$this->response("Some problems occurred, please try again.", REST_Controller::HTTP_BAD_REQUEST);
}
}else{
//set the response and exit
$this->response([
'status' => FALSE,
'message' => 'No user were found.'
], REST_Controller::HTTP_NOT_FOUND);
}
}
}
?>
Точно так же, как этот учебник