【问题标题】:Codeigniter Bootstrap RESTServer - How to set up routing?Codeigniter Bootstrap RESTServer - 如何设置路由?
【发布时间】:2014-03-23 18:04:18
【问题描述】:

我正在做这个教程:http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814(当然),我在 Github-Repo (CodeIgniter-Bootstrap) 上找到了 CodeIgniter 和 Bootstrap。我只是不明白为什么我不能通过 REST-URL 访问我的 REST-Server。任何教程都没有很好地提及路由。

这是我在应用程序/控制器目录中的 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
    }
}

我写进routes.php:

$route['players'] = "players";

这是我在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-Request:

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!");
        }
    });
}

这是我的第一个 CodeIgniter 项目和第一个 REST-API,所以如果有人可以提供帮助,我将非常感激。我忽略了什么?我几个小时就坐在这个上面! 非常感谢每一个有用的答案!

【问题讨论】:

  • 你需要在你的控制器中添加播放器方法

标签: php ajax codeigniter rest url-routing


【解决方案1】:

在您的代码中:

 $.ajax({
       url: "http://myproject.cloudcontrolled.com/players/player",

它将数据发送到“玩家”控制器“玩家”方法,如果你想使用索引方法改变你的路线

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

【讨论】:

  • 谢谢!我玩了很多,这只是一个剩菜。只是为了我的理解。如果我在浏览器中输入 myproject.cloudcontrolled.com/players/index 它应该返回“它可以工作”,还是不返回?
猜你喜欢
  • 1970-01-01
  • 2016-06-18
  • 2012-06-21
  • 2016-08-18
  • 2011-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-28
相关资源
最近更新 更多