【问题标题】:I am trying to implement a REST-API with CodeIgniter and RestServer on CloudControl我正在尝试在 CloudControl 上使用 CodeIgniter 和 RestServer 实现 REST-API
【发布时间】:2014-05-01 03:12:33
【问题描述】:

我不明白。我必须进行哪些设置才能在我的 cloudcontrol URL 上运行 REST-API。我制作了一个名为 player 的控制器:

<?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'] = '';

我正在使用 MobileBoilerplate 中的 .htaccess 并像这样更改了 RewriteBase

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
  # Options +SymLinksIfOwnerMatch
    RewriteEngine On
    RewriteBase /
</IfModule>

我必须对路由进行特殊设置吗?如何在 CloudControl 上做到这一点?是不是还有一个错误?当我尝试访问 myproject.cloudcontrolled.com/players/index 时收到 404

感谢您的帮助。

【问题讨论】:

    标签: php .htaccess codeigniter rest cloudcontrol


    【解决方案1】:

    我需要更多信息来帮助您:

    1. 您是否根据此文档设置了根目录? https://github.com/cloudControl/buildpack-php#manually-setting-the-documentroot
    2. 如果是这样,请将测试文件放在根目录中,然后尝试通过浏览器打开它。
    3. 您是否部署了您的应用程序?
    4. 你的错误日志说什么?

    【讨论】:

    • 1.不...这可能是问题所在。我在 .htaccess 中做了一些设置,但我猜这是错误的。 Buildpack 确实是找到一些示例的好地方。正如我所说,我使用了 MobileBoilerplate-htaccess。谢谢,但我需要先完成一些事情才能回到 REST-API。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    相关资源
    最近更新 更多