在此服务器上找不到请求的 URL /~paul/CodeIgniter/api/example/user/
The requested URL /~paul/CodeIgniter/api/example/user/ was not found on this server
我在网上关注了这个 tutorial,我目前正在使用 High Sierra MacOS。我尝试使用 PHP 进行简单的 restAPI,现在我正尝试在 CodeIgniter3.x
框架上实现它。我创建的可以成功 POST 并在 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>
我上次自己创建的时候用的URLAPI是这样的
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= [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/ [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);
}
}
}
?>
和这个一模一样tutorial
您有 case sensitivity
个问题。在错误中,它要求 CodeIgniter
而您的实际目录名为 codeigniter
.
终于解决了!! .我所做的是
从这个URL/~paul/CodeIgniter/api/example/user/
我改成了这个/~paul/codeigniter/index.php/api/example/user/
并从 restserver
添加了一些东西到 api/example.php
require APPPATH . '/libraries/REST_Controller.php';
require APPPATH . '/libraries/Format.php';
use Restserver\Libraries\REST_Controller;
现在可以用了!
我在网上关注了这个 tutorial,我目前正在使用 High Sierra MacOS。我尝试使用 PHP 进行简单的 restAPI,现在我正尝试在 CodeIgniter3.x
框架上实现它。我创建的可以成功 POST 并在 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>
我上次自己创建的时候用的URLAPI是这样的
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= [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/ [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);
}
}
}
?>
和这个一模一样tutorial
您有 case sensitivity
个问题。在错误中,它要求 CodeIgniter
而您的实际目录名为 codeigniter
.
终于解决了!! .我所做的是
从这个URL/~paul/CodeIgniter/api/example/user/
我改成了这个/~paul/codeigniter/index.php/api/example/user/
并从 restserver
require APPPATH . '/libraries/REST_Controller.php';
require APPPATH . '/libraries/Format.php';
use Restserver\Libraries\REST_Controller;
现在可以用了!