maltyxx/restserver

此包已被弃用且不再维护。没有建议的替代包。

Codeigniter 2 和 Codeigniter 3 的 REST 全功能服务器

v2.0.14-alpha 2019-04-01 13:03 UTC

README

Codeigniter 3 的 REST 全功能服务器

需求

  • PHP 5.3.x (Composer 需求)
  • CodeIgniter 3.x

安装

步骤 1 使用 Composer 安装

运行 composer

composer require maltyxx/restserver

步骤 2 创建文件

/application/core/MY_Controller.php 中创建控制器文件。

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

class MY_Controller extends CI_Controller
{
    public function __construct() {
        parent::__construct();
    }
}

require(FCPATH.'vendor/maltyxx/restserver/core/Restserver_controller.php');

仅当您使用 Form_validation 时。

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require(FCPATH.'vendor/maltyxx/restserver/libraries/MY_Form_validation.php');

步骤 3 配置

./application/config/restserver.php 中复制配置文件 ./application/vendor/maltyxx/restserver/config/restserver.php

步骤 4 规则

// Setting Rules Using an Array
$config = array(
    array(
            'field' => 'username',
            'label' => 'Username',
            'rules' => 'required'
    ),
    array(
            'field' => 'password',
            'label' => 'Password',
            'rules' => 'required',
            'errors' => array(
                    'required' => 'You must provide a %s.',
            ),
    ),
    array(
            'field' => 'passconf',
            'label' => 'Password Confirmation',
            'rules' => 'required'
    ),
    array(
            'field' => 'email',
            'label' => 'Email',
            'rules' => 'required'
    )
);

$this->restserver->set_rules($config);

// More exemples
https://www.codeigniter.com/user_guide/libraries/form_validation.html?highlight=form

步骤 5 示例

控制器文件位于 ./application/controllers/Server.php

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

class Server extends Restserver_Controller {

    public function __construct() {
        parent::__construct();
        
        // Configuration
        $config = array(
            array(
                // Nom entrant (requis)
                'field' => 'lastname',

                // Modélisation interne (option)
                'alias' => 'user.lastname|famille.pere.nom',

                // Nom du champ (option)
                'label' => 'Nom',

                // Les règles (option)
                'rules' => 'required_post|alpha|min_length[2]|max_length[250]',

                // Documentation (option)
                'comment' =>
                    "Input: lastname".PHP_EOL.
                    "Label: Nom de famille".PHP_EOL.
                    "Type: string (min 2, max 250 caractères)".PHP_EOL.
                    "Requis: POST"
            )
        );
        
        $this->restserver->set_rules($config);
    }

    /**
     * Méthode POST
     */
    public function post() {
        // ---------- Exemple de récupération
        // Récupération du champ entrant
        $lastname = $this->restserver->post('lastname');
        
        // Récupération du champ modélisé
        $alias = $this->restserver->alias();
        
        // Espace de nom 1
        $lastname = $alias['user']['lastname'];
        
        // Espace de nom 2
        $lastname = $alias['famille']['pere']['nom'];
        
        // ---------- Réponse
        $response = $this->restserver->protocol();
        $response['status'] = TRUE;
        $response['error'] = NULL;
        $response['value'] = array(
            'lastname' => $lastname
        );
        
        // Envoi la réponse avec le code HTTP 201 Created
        $this->restserver->response($response, 201);
    }
    
    /**
     * Méthode GET
     */
    public function get() {        
        $this->restserver->response();
    }
        
    /**
     * Méthode PUT
     */
    public function put() {
        $this->restserver->response();
    }
    
    /**
     * Méthode DELETE
     */
    public function delete() {
        $this->restserver->response();
    }
}