在此服务器上找不到请求的URL /〜paul / CodeIgniter / api / example / user /

时间:2018-11-05 18:59:32

标签: php macos codeigniter macos-high-sierra

我在线上关注了此tutorial,目前正在使用High Sierra MacOS。我使用PHP尝试了一个简单的restAPI,现在尝试在CodeIgniter3.x框架上实现它。我创建的代码可以在POSTMAN上成功进行POST和GET,但是现在我尝试按照本教程操作,却在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>

我上次创建自己的API时使用的URL如下

http://localhost/~paul/v1/login.php

我认为High Sierra MacOS上的CodeIgniter设置存在问题吗?

有人可以帮我吗。谢谢

编辑:如果该信息不足,请通知我。

enter image description here

编辑:我尝试了区分大小写的问题

enter image description here

编辑:添加的信息

这是我的.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);
    }
}  
}

?>

与此tutorial

完全相同

2 个答案:

答案 0 :(得分:1)

您遇到case sensitivity问题。在错误中,当您的实际目录名为CodeIgniter时,它询问了codeigniter

答案 1 :(得分:0)

这终于解决了! 。我所做的是

从此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;

现在就可以了!