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

2023-11-25

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

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

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

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

消息:类示例无法从特征扩展 Restserver\库\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();


您必须修改提供的(并且在此答案日期之前已过时)版本application\libraries\REST_Controller.php and application\controllers\api\Example.php.

应用程序\库\REST_Controller.php

  • Add require APPPATH . 'libraries/REST_Controller_Definitions.php';就在之前trait REST_Controller {
require APPPATH . 'libraries/REST_Controller_Definitions.php';

trait REST_Controller {

应用程序\控制器\api\Example.php

  • class Example extends CI_Controller {代替class Example extends REST_Controller {
  • Place use REST_Controller { REST_Controller::__construct as private __resTraitConstruct; }作为之后的第一行class Example extends CI_Controller {
  • Add parent::__construct(); and $this->__resTraitConstruct(); to __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' => '[email protected]', 'fact' => 'Loves coding'],
            ['id' => 2, 'name' => 'Jim', 'email' => '[email protected]', 'fact' => 'Developed on CodeIgniter'],
            ['id' => 3, 'name' => 'Jane', 'email' => '[email protected]', '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
    }

}

希望它有帮助,它对我有用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

类示例无法从特征 Restserver\Libraries\REST_Controller 扩展 的相关文章

  • 在php中获取二进制数据大小的正确方法是什么?

    我已阅读文件的一部分 现在想确保该部分的大小正确 我怎样才能在 php 中做到这一点 part fread file 1024 return some function part 1024 我已经阅读了这些示例 但我怀疑是否要使用 strl
  • 当存在联系时如何为数组分配排名号

    当尝试为数组中存在平局的数值分配排名时 我很难知道从哪里开始 因此 例如 我需要像下面这样转换一个数组 myarray 4 76 34 13 34 到另一个数组中 例如 myarray2 1 5 3 5 2 3 5 基本上 当相同的数字在数
  • 如何将 Filesystem 类的 glob 方法与 StorageFacade 结合使用?

    这涉及到拉拉维尔 5 我可以看到Illuminate Filesystem Filesystem一个方法叫做glob pattern flags 0 不幸的是 这个方法并没有体现在默认的情况下FilesystemAdapter随 Larav
  • mongodb对话系统

    我正在实施一个verymongodb 上的简单对话系统 这个想法应该是 当我打开一个 convo 时 它应该显示发送和接收的消息 到目前为止一切正常 并且应该非常容易 通过使用像这样的伪代码这样的简单查询 from my id AND to
  • 如何在Web服务中传递URL

    我想将此 URL 作为网址中的值传递http localhost h2orn php verify php email emails hash hash但是 我只能在 符号之前传递 我想传递所有 URL 我正在使用java网络服务 代码在这
  • 如何在 Laravel 5.5 中编辑“页面因不活动而已过期”的视图

    在 Laravel 5 5 中 当您使用 CSRF 保护 默认情况下 并且在长时间不活动后发送发布请求时 您将收到此错误页面 屏幕截图 我对此错误表示同意 但是 我需要更改此错误的视图 文本以确保与我的应用程序风格和语言相匹配 关于如何编辑
  • Laravel 5 与 SAML 2 和现有 IDP 集成

    我使用 Laravel 5 我正在尝试将 SAML 2 0 与其集成 我找到了这个包 https github com aacotroneo laravel saml2 https github com aacotroneo laravel
  • SimpleSAMLPHP 重定向循环

    我们正在尝试使用自定义 mysql 数据库设置 sso 但它在以下两个请求之间进入无限循环 POST http 192 168 0 15 simplesaml module php core loginuserpass php 设置Cook
  • Propel Query 中的动态表名称

    我想知道您是否可以使 propel 查询的表名称动态化 有点像变量 一个例子类似于 DynamicVar Query create 我让它在 ifs 中工作 就像下面的例子一样 但如果更动态地制作 可以删除相当多的行 这些表的设置都是相同的
  • 如何在 Laravel 5 中处理嵌套的 JSON 对象请求?

    我们在 Laravel 5 和 AngularJs Ionic 中运行此 Web 服务来处理 Web 当我们将请求从 Web 客户端 发送到 Web 服务 后端 时 我们传递了嵌套的 JSON 对象 我们在读取服务器端父对象下的所有子对象时
  • CSV 从 UTF8 到 ISO-8859-1

    我正在尝试修改我的 CSV 导出 但它不会将我的 CSV 从 UTF 8 转换 保存为 ISO 8859 1 请问我做错了什么吗 实际上自从修改了这个之后 我得到了一个空的 CSV 文件 php 7 0 x function my Gene
  • Yii2 DropDownList Onchange 更改自动完成小部件“源”属性?

    我已经尝试过这个 yii2 依赖的自动完成小部件 https stackoverflow com questions 27025791 yii2 dependent autocomplete widget 但我不知道为什么它不起作用 这是我
  • 疯狂的 crond 行为。不断使 bash 进程失效

    我有一个看起来像这样的 crontab SHELL bin bash PATH sbin bin usr sbin usr bin MAILTO root HOME 0 59 var www html private fivemin zda
  • ZF2 工厂获取参数

    我有一个动态类别导航 在导航工厂中 我想从路线获取参数 我怎样才能做到这一点 在我看来 在我的 module php 中 public function getServiceConfig return array factories gt
  • 解析 PHP 响应:未捕获的语法错误:意外的标记 <

    我正在使用 AJAX 来调用 PHP 脚本 我唯一需要从响应中解析的是脚本生成的随机 ID 问题是 PHP 脚本会引发许多错误 这些错误实际上很好 不会妨碍程序功能 唯一的问题是当我跑步时 parseJSON response I get
  • 避免 SQLite3 中的 SQL 注入

    我正在尝试找出一种避免 SQL 注入的好简单方法 到目前为止我只能提出两个想法 对用户输入进行 Base64 编码 其实不想这样做 使用正则表达式删除不需要的字符 目前正在使用这个 不确定是否100 安全 这是我当前的代码
  • 如何使用 PDO 动态构建查询

    我正在使用 PDO 并想做这样的事情 query dbh gt prepare SELECT FROM table WHERE column value query gt bindParam table tableName query gt
  • 使 div 的大小与其内部图像的大小相同

    我有一个带有以下代码的div HTML div img src img logo png div CSS div imgContainer width 250px height 250px padding 13px 问题是用户可以编辑图像大
  • 如何显示不同页眉的页面? [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 我正在为我的学校项目开发网站 但我遇到了一个问题 我在每个页面上显示一个标题 我的标题之一包含登录表单 另一标题包含用户名 搜索栏等 问题是
  • Laravel 5 注销特定用户

    在我的 laravel 5 应用程序中 有一个功能允许具有管理员角色的用户重置非管理员的任何人的密码 但这不会强制该人注销并再次登录 更改密码后如何强制用户注销 我没有对用于验证用户身份或任何内容的中间件进行任何更改 我不知道它是否有效 但

随机推荐