类示例不能从特征Restserver \ Libraries \ REST_Controller扩展

时间:2019-06-30 10:14:34

标签: php codeigniter

我正在尝试在新项目中实现以下库。

  

“ chriskacerguis / codeigniter-restserver”:“ ^ 3.0”

我在本地服务器上安装了全新的codeigniter,并已相应地完成了所有操作。 现在,我正在尝试运行代码,仅显示以下错误

  

致命错误:类示例无法从特征扩展   Restserver \ Libraries \ REST_Controller在   第22行的C:\ xampp \ htdocs \ ci \ application \ controllers \ api \ Example.php   遇到PHP错误严重性:编译错误

     

消息:类示例无法从特征扩展   Restserver \ Libraries \ REST_Controller

     

文件名:api / Example.php

     

行号:22

     

回溯:

第22行的代码如下

<?php
use Restserver\Libraries\REST_Controller;
defined('BASEPATH') OR exit('No direct script access allowed');

// Following line is line no 22
class Example extends REST_Controller {
    function __construct()
    {
        // Construct the parent class
        parent::__construct();

1 个答案:

答案 0 :(得分:4)

您必须修改application\libraries\REST_Controller.phpapplication\controllers\api\Example.php的提供的版本(直到该答案的日期为止)。

应用程序\库\ REST_Controller.php

  • require APPPATH . 'libraries/REST_Controller_Definitions.php';之前添加trait REST_Controller {
require APPPATH . 'libraries/REST_Controller_Definitions.php';

trait REST_Controller {

application \ controllers \ api \ Example.php

  • class Example extends CI_Controller {而非class Example extends REST_Controller {
  • use REST_Controller { REST_Controller::__construct as private __resTraitConstruct; }放在class Example extends CI_Controller {之后的第一行
  • parent::__construct();$this->__resTraitConstruct();添加到__construct()函数中。
  • 将所有响应方法更改为HTTP响应代码,而不是常量。例如,用$this->response($users, 200);代替$this->response($users, REST_Controller::HTTP_OK);
<?php
use Restserver\Libraries\REST_Controller;
defined('BASEPATH') OR exit('No direct script access allowed');

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
/** @noinspection PhpIncludeInspection */

//To Solve File REST_Controller not found
require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';

/**
 * This is an example of a few basic user interaction methods you could use
 * all done with a hardcoded array
 *
 * @package         CodeIgniter
 * @subpackage      Rest Server
 * @category        Controller
 * @author          Phil Sturgeon, Chris Kacerguis
 * @license         MIT
 * @link            https://github.com/chriskacerguis/codeigniter-restserver
 */
class Example extends CI_Controller {

    use REST_Controller {
        REST_Controller::__construct as private __resTraitConstruct;
    }

    function __construct()
    {
        // Construct the parent class
        parent::__construct();
        $this->__resTraitConstruct();

        // Configure limits on our controller methods
        // Ensure you have created the 'limits' table and enabled 'limits' within application/config/rest.php
        $this->methods['users_get']['limit'] = 500; // 500 requests per hour per user/key
        $this->methods['users_post']['limit'] = 100; // 100 requests per hour per user/key
        $this->methods['users_delete']['limit'] = 50; // 50 requests per hour per user/key
    }

    public function users_get()
    {
        // Users from a data store e.g. database
        $users = [
            ['id' => 1, 'name' => 'John', 'email' => 'john@example.com', 'fact' => 'Loves coding'],
            ['id' => 2, 'name' => 'Jim', 'email' => 'jim@example.com', 'fact' => 'Developed on CodeIgniter'],
            ['id' => 3, 'name' => 'Jane', 'email' => 'jane@example.com', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]],
        ];

        $id = $this->get('id');

        // If the id parameter doesn't exist return all the users

        if ($id === null)
        {
            // Check if the users data store contains users (in case the database result returns NULL)
            if ($users)
            {
                // Set the response and exit
                $this->response($users, 200); // OK (200) being the HTTP response code
            }
            else
            {
                // Set the response and exit
                $this->response([
                    'status' => false,
                    'message' => 'No users were found'
                ], 404); // NOT_FOUND (404) being the HTTP response code
            }
        }

        // Find and return a single record for a particular user.

        $id = (int) $id;

        // Validate the id.
        if ($id <= 0)
        {
            // Invalid id, set the response and exit.
            $this->response(null, 400); // BAD_REQUEST (400) being the HTTP response code
        }

        // Get the user from the array, using the id as key for retrieval.
        // Usually a model is to be used for this.

        $user = null;

        if (!empty($users))
        {
            foreach ($users as $key => $value)
            {
                if (isset($value['id']) && $value['id'] === $id)
                {
                    $user = $value;
                }
            }
        }

        if (!empty($user))
        {
            $this->set_response($user, 200); // OK (200) being the HTTP response code
        }
        else
        {
            $this->set_response([
                'status' => false,
                'message' => 'User could not be found'
            ], 404); // NOT_FOUND (404) being the HTTP response code
        }
    }

    public function users_post()
    {
        // $this->some_model->update_user( ... );
        $message = [
            'id' => 100, // Automatically generated by the model
            'name' => $this->post('name'),
            'email' => $this->post('email'),
            'message' => 'Added a resource'
        ];

        $this->set_response($message, 201); // CREATED (201) being the HTTP response code
    }

    public function users_delete()
    {
        $id = (int) $this->get('id');

        // Validate the id.
        if ($id <= 0)
        {
            // Set the response and exit
            $this->response(null, 400); // BAD_REQUEST (400) being the HTTP response code
        }

        // $this->some_model->delete_something($id);
        $message = [
            'id' => $id,
            'message' => 'Deleted the resource'
        ];

        $this->set_response($message, 204); // NO_CONTENT (204) being the HTTP response code
    }

}

希望它对我有用。