Search code examples
phpmacoscodeignitermacos-high-sierra

The requested URL /~paul/CodeIgniter/api/example/user/ was not found on this server


I followed this tutorial online and I'm currently using High Sierra MacOS . I tried a simple restAPI using PHP and now I'm trying to implement it on a CodeIgniter3.x framework. The one I created can successfully POST and GET on POSTMAN but now that I tried following the tutorial I had this error on 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>

the URL I am using last time when I created my own API is like this

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

I think there's a problem on the setup of my CodeIgniter on the High Sierra MacOS?

Could someone please help me out. Thank you

EDIT: If the information does not suffice please let me know.

enter image description here

EDIT: I tried the case sensitivity problem

enter image description here

EDIT: Added Information

Here's my .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] 

and my 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);
    }
}  
}

?>

Exactly the same as this tutorial


Solution

  • This has been solved finally!! . What I did was

    from this URL /~paul/CodeIgniter/api/example/user/ I changed it to this /~paul/codeigniter/index.php/api/example/user/ and added some stuff on api/example.php from restserver

    require APPPATH . '/libraries/REST_Controller.php';
    require APPPATH . '/libraries/Format.php';
    
    use Restserver\Libraries\REST_Controller;
    

    and now It works!