Timo
Timo

Reputation: 320

I am trying to implement a REST-API with CodeIgniter and RestServer on CloudControl

I am not getting it. What settings do I have to make to get the REST-API running on my cloudcontrol URL. I made a controller named 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
    }
}

This I put in the config.php:

$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
$config['index_page'] = '';

I am using the .htaccess from MobileBoilerplate and changed RewriteBase like this

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

Do I have to make special settings for the routing? How can I do that on CloudControl? Is there another mistake? I am getting 404 when I try to access myproject.cloudcontrolled.com/players/index

Thanks for any help.

Upvotes: 2

Views: 528

Answers (1)

sre
sre

Reputation: 415

I would need some more information to help you:

  1. Did you set your root directory accordingly to this doc? https://github.com/cloudControl/buildpack-php#manually-setting-the-documentroot
  2. If so, put a test file in your root dir and try opening it via the browser.
  3. Did you deploy your app?
  4. What does you error log say?

Upvotes: 2

Related Questions