daycry / restserver
Requires
- php: >=7.4 || ^8.0
- daycry/class-finder: ^2.2
- daycry/cronjob: ^2.0
- daycry/encryption: ^2.0
- daycry/jwt: ^1.0
- daycry/relations: ^2.0
- daycry/settings: ^1.0
Requires (Dev)
- codeigniter/coding-standard: ^1.5
- codeigniter4/framework: ^4
- fakerphp/faker: ^1.9
- friendsofphp/php-cs-fixer: 3.13.0
- kint-php/kint: ^5.0.3
- mikey179/vfsstream: ^1.6
- nexusphp/cs-config: ^3.6
- nexusphp/tachycardia: ^1.0
- php-coveralls/php-coveralls: ^2.5
- phpstan/phpstan: ^1.7.1
- phpunit/phpcov: ^8.2
- phpunit/phpunit: ^9.1
- predis/predis: ^1.1 || ^2.0
- rector/rector: 0.15.12
- vimeo/psalm: ^5.0
- dev-development
- v7.2.10
- v7.2.9
- v7.2.8
- v7.2.7
- v7.2.6
- v7.2.5
- v7.2.4
- v7.2.3
- v7.2.2
- v7.2.1
- v7.2.0
- v7.1.1
- v7.1.0
- v7.0.9
- v7.0.8
- v7.0.7
- v7.0.6
- v7.0.5
- v7.0.4
- v7.0.3
- v7.0.2
- v7.0.1
- v7.0.0
- v6.2.2
- v6.2.1
- v6.2.0
- v6.1.1
- v6.1.0
- v6.0.4
- v6.0.3
- v6.0.2
- v6.0.1
- v6.0.0
- v5.0.0
- v4.0.21
- v4.0.20
- v4.0.19
- v4.0.18
- v4.0.17
- v4.0.16
- v4.0.15
- v4.0.14
- v4.0.13
- v4.0.12
- v4.0.11
- v4.0.10
- v4.0.9
- v4.0.8
- v4.0.7
- v4.0.6
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- v3.0.19
- v3.0.18
- v3.0.17
- v3.0.16
- v3.0.15
- v3.0.14
- v3.0.13
- v3.0.12
- v3.0.11
- v3.0.10
- v3.0.9
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.0.8
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.0.20
- v1.0.19
- v1.0.18
- v1.0.17
- v1.0.16
- v1.0.15
- v1.0.14
- v1.0.13
- v1.0.12
- v1.0.11
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-master
This package is auto-updated.
Last update: 2023-05-04 09:23:07 UTC
README
REST服务器
Codeigniter 4的带有Doctrine的REST服务器
版本7
此版本与之前的版本不兼容,因为它包含新的自动化功能、数据库重构(使用新的外键),因此如果更新,请检查表名是否使用复数。
通过composer安装
使用composer install安装此包
> composer require daycry/restserver
手动安装
下载此仓库,然后通过编辑
$psr4 = [ 'Config' => APPPATH . 'Config', APP_NAMESPACE => APPPATH, 'App' => APPPATH, 'Daycry\RestServer' => APPPATH .'ThirdParty/restserver/src', ];
配置
运行命令
> php spark restserver:publish
> php spark settings:publish
> php spark cronjob:publish
> php spark jwt:publish
此命令将复制一个配置文件到您的app命名空间。然后您可以根据需要调整它。默认文件将位于app/Config/RestServer.php
。
> php spark migrate -all
此命令将在您的数据库中创建REST服务器表。
如果您想加载示例种子,可以使用此命令。
> php spark db:seed Daycry\RestServer\Database\Seeds\ExampleSeeder
有关安装doctrine的更多信息:https://github.com/daycry/doctrine
使用加载库
<?php namespace App\Controllers; class Center extends \Daycry\RestServer\RestServer { public function index() { return $this->respond( $this->content ); } }
如果您想在控制器调用之前更改属性
<?php namespace App\Controllers; class Center extends \Daycry\RestServer\RestServer { public function __construct() { $this->_restConfig = config('RestServer'); $this->_restConfig->restAjaxOnly = true; } public function index() { return $this->respond( $this->content ); } }
如果您需要验证数据,您可以调用validation
方法,传入字符串规则和您需要的验证配置文件。
例如:app/Config/Validation.php
或如果规则在自定义命名空间中,app/Modules/Example/Config/Validation.php
public $requiredLogin = [ 'username' => 'required', 'password' => 'required', //'fields' => 'required' ];
<?php namespace App\Controllers; class Center extends \Daycry\RestServer\RestServer { public function index() { $this->validation( 'requiredLogin' ); return $this->respond( $this->content ); } }
$this->content包含请求中的主体内容。$this->args包含所有参数,get、post、headers等,但您也可以使用对象$this->request来获取这些参数。
<?php namespace App\Controllers; class Center extends \Daycry\RestServer\RestServer { public function index() { $this->validation( 'requiredLogin', config( Example\\Validation ), true, true ); return $this->respond( $this->content ); } }
验证函数参数
字段 | 描述 |
---|---|
规则 | 规则名称 |
命名空间 | 包含规则的命名空间 |
getShared | true或 |
过滤器 | 如果您想限制主体内容仅限于规则的参数。 |
用户模型类
默认情况下,您可以通过'\Daycry\RestServer\Models\UserModel'模型将用户与键关联起来,但您可以通过创建现有的'\Daycry\RestServer\Libraries\User\UserAbstract'类来自定义它。
示例
<?php namespace App\Models; use Daycry\RestServer\Libraries\User\UserAbstract; class CustomUserModel extends UserAbstract { protected $DBGroup = 'default'; protected $table = 'users'; protected $primaryKey = 'id'; protected $useAutoIncrement = true; protected $returnType = 'object'; protected $useSoftDeletes = true; protected $allowedFields = [ 'name', 'key_id' ]; protected $useTimestamps = true; protected $createdField = 'created_at'; protected $updatedField = 'updated_at'; protected $deletedField = 'deleted_at'; protected $validationRules = []; protected $validationMessages = []; protected $skipValidation = false; }
如果您自定义用户类,您必须修改默认配置
示例
<?php public $restUsersTable = 'restserver_user'; //user table name public $userModelClass = \Daycry\RestServer\Models\UserModel::class; //user model Class public $userKeyColumn = 'key_id'; // column that associates the key
异常 & 阻止无效尝试
如果您想使用某些自定义异常将其用作失败的请求尝试并允许阻止该IP,您必须创建静态属性 authorized。
如果 authorized 为 false,系统将增加该IP的失败尝试次数。示例
<?php namespace App\Exceptions; use CodeIgniter\Exceptions\FrameworkException; class SecretException extends FrameworkException { protected $code = 401; public static $authorized = true; public static function forInvalidPassphrase() { self::$authorized = false; return new self(lang('Secret.invalidPassphrase')); } public static function forInvalidToken() { self::$authorized = false; return new self(lang('Secret.invalidToken')); } public static function forExpiredToken() { self::$authorized = false; return new self(lang('Secret.tokenExpired')); } public static function forTokenReaded() { self::$authorized = false; return new self(lang('Secret.readed')); } }
OPTIONS
您可以使用 petition
表独立地自定义请求。
字段 | 值 | 选项 | 描述 |
---|---|---|---|
namespace_id |
此字段包含 ws_namespaces 表的标识符,此表存储类的命名空间,例如 \App\Controllers\Auth |
||
method |
login |
使用此字段来配置控制器的操作方法 | |
http |
post |
get ,post ,put ,patch ,options |
使用此字段来配置控制器的操作方法 |
auth |
bearer |
false ,basic ,digest ,bearer |
使用此字段来配置认证方法 |
log |
null |
null ,1 ,0 |
如果您想记录请求,请使用此字段 |
limit |
null |
null ,1 ,15 |
如果您想设置请求限制,请使用此字段,此值必须为整数 |
time |
null |
null ,1 ,15 |
此字段用于了解请求限制重置的频率(以秒为单位,例如:3600 -> 在这种情况下,您可以在3600秒内执行{limit}次请求) |
level |
null |
null ,1 ,10 |
使用此字段来表示请求的权限级别,如果令牌级别为1,而请求级别为3,则您将无法执行该请求 |
您可以使用命令自动填充 ws_namespaces。
<?php
php spark restserver:discover
此命令在您指定的命名空间或命名空间中搜索类。您可以在 RestServer.php 配置文件中设置此命名空间。
<?php /** *-------------------------------------------------------------------------- * Cronjob *-------------------------------------------------------------------------- * * Set to TRUE to enable Cronjob for fill the table petitions with your API classes * $restNamespaceScope \Namespace\Class or \Namespace\Folder\Class or \Namespace example: \App\Controllers * * This feature use Daycry\CronJob vendor * for more information: https://github.com/daycry/cronjob * */ public string $restApiTable = 'ws_apis'; public string $restNamespaceTable = 'ws_namespaces'; public array $restNamespaceScope = ['\App\Controllers', '\Api\Controllers'];
或者创建一个cronjob任务,编辑 CronJob.php 配置文件,如下所示。
<?php /* |-------------------------------------------------------------------------- | Cronjobs |-------------------------------------------------------------------------- | | Register any tasks within this method for the application. | Called by the TaskRunner. | | @param Scheduler $schedule */ public function init(Scheduler $schedule) { $schedule->command('restserver:discover')->named('discoverRestserver')->daily(); or $schedule->command('restserver:discover')->named('discoverRestserver')->daily('11:30 am'); }
有关cronjob的更多信息: https://github.com/daycry/cronjob
响应
默认响应为 json
,但您可以在标题中将其更改为 xml
。
Accept: application/json
或
Accept: application/xml
或您可以在GET变量中设置格式
http://example.com?format=json
http://example.com?format=xml
请求数据体
请求数据体默认为 json
,但您可以更改它。
Content-Type: application/json
或
Content-Type: application/xml
API令牌
您可以将 api rest token
发送至标题、GET或POST变量,如下所示。
X-API-KEY: TOKEN
http://example.com?X-API-KEY=key
语言
您可以通过这种方式发送 language
。
Accept-Language: en