Php Codeigniter引导RESTServer-如何设置路由?

Php Codeigniter引导RESTServer-如何设置路由?,php,ajax,codeigniter,rest,url-routing,Php,Ajax,Codeigniter,Rest,Url Routing,我正在做这个教程:(当然)我在Github Repo(CodeIgniter Bootstrap)上找到了CodeIgniter和Bootstrap。我只是不明白为什么我不能通过RESTURL访问我的REST服务器。任何教程都没有很好地提到布线 这是应用程序/控制器目录中的my Rest Controller player.php: <?php defined('BASEPATH') OR exit('No direct script access allowed'); require(

我正在做这个教程:(当然)我在Github Repo(CodeIgniter Bootstrap)上找到了CodeIgniter和Bootstrap。我只是不明白为什么我不能通过RESTURL访问我的REST服务器。任何教程都没有很好地提到布线

这是应用程序/控制器目录中的my Rest Controller player.php:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

require(APPPATH'.libraries/REST_Controller.php');

class Players extends REST_Controller {

    function index() {
        echo 'It works';
    }

    public function players_get() {
        $this->response($this->db->select('playerName')->result());
    }

    public function player_get() {
        if(!$this->get('playerName')) {
            $this->response(NULL, 400);
        }
        $playerName = $this->input->get('playerName');
        $this->response($this->db
            ->select('playerName')
            ->from('players')
            ->where('playerName', $playerName)
            ->get()
        );
    }

    public function player_post() {
        $playerName = $this->input->post('playerName');
        $password = $this->input->post('password');
        $player = array(
           'playerName' => $playerName,
           'password' => $password
        );
        // INSERT INTO 'players' (playerName, password) VALUES ($playerName, $password);
        $this->db->insert('players', $player);
        // On success, send back array with data
        $this->response($player, 201); // Send an HTTP 201 Created
        // On fail, send empty array
        $this->response(array()); // HTTP 404 Not Found
    }
}
这是我在config.php中写的内容:

$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
$config['index_page'] = '';
我还没有模型。如果我可以通过以下url访问API,我只想尝试一下:

myproject.cloudcontrolled.com/players

。我想,至少它会向我显示index()函数中的回声。但我得到的只是404。 最后,我需要做的是通过$.ajax发送POST请求:

function myRegSubmit() {
    $.ajax({
        url: "http://myproject.cloudcontrolled.com/players/player",
        data: {
            playerName: $("#inputPlayer").val(),
            password: $("#inputPassword").val()
        },
        type: "POST",
        dataType: "json",
        // code to run if the request succeeds;
        // the response is passed to the function
        success: function (json) {
            $("#errorSpan").append(" It worked!");
            //$( "<h1/>" ).text( json.title ).appendTo( "body" );
            //$( "<div class=\"content\"/>").html( json.html ).appendTo( "body" );
        },
        // code to run if the request fails; the raw request and
        // status codes are passed to the function
        error: function (xhr, status) {
            $("#errorSpan").append(" Sorry, there was a problem!");
        },
        // code to run regardless of success or failure
        complete: function (xhr, status) {
            $('#errorSpan').append(" The request is sent!");
        }
    });
}
函数myRegSubmit(){
$.ajax({
url:“http://myproject.cloudcontrolled.com/players/player",
数据:{
playerName:$(“#inputPlayer”).val(),
密码:$(“#输入密码”).val()
},
类型:“POST”,
数据类型:“json”,
//请求成功时要运行的代码;
//响应被传递给函数
成功:函数(json){
$(“#errorSpan”).append(“它成功了!”);
//$(“”).text(json.title).appendTo(“body”);
//$(“”).html(json.html).appendTo(“body”);
},
//请求失败时要运行的代码;原始请求和
//状态代码被传递给函数
错误:函数(xhr,状态){
$(“#errorSpan”).append(“对不起,出了问题!”);
},
//无论成功或失败都要运行的代码
完成:功能(xhr,状态){
$(“#errorSpan”).append(“请求已发送!”);
}
});
}
这是我的第一个CodeIgniter项目和第一个REST-API,所以如果有人能帮忙,我将非常感激。我忽略了什么?我已经坐了好几个小时了! 非常感谢每一个有用的答案

在您的代码中:

 $.ajax({
       url: "http://myproject.cloudcontrolled.com/players/player",
它将数据发送到“玩家”控制器“玩家”方法,如果您想使用索引方法更改您的路线

$route['players/(:any)'] = "players"

你需要在你的控制器中添加玩家方法谢谢!我玩了很多次,这只是一个剩菜。只是为了我的理解。如果我在浏览器myproject.cloudcontrolled.com/players/index中输入,它应该返回“it works”,还是不返回?
$route['players/(:any)'] = "players"