daycry / restful
Codeigniter 4 的 Restful 服务器
Requires
- php: >=7.4 || ^8.0
- daycry/class-finder: ^2.0
- daycry/cronjob: ^2.0
- daycry/encryption: ^2.0
- daycry/exceptions: *
- daycry/jwt: ^1.0
- daycry/settings: ^1
Requires (Dev)
- codeigniter4/devkit: ^1
- codeigniter4/framework: ^4
- mikey179/vfsstream: ^1
- mockery/mockery: ^1
- rector/rector: 0.15.12
This package is auto-updated.
Last update: 2024-09-23 17:00:08 UTC
README
Restful
Codeigniter 4 的 Restful 服务器
通过 composer 安装
使用 composer install 命令安装包
> composer require daycry/restful
配置
运行命令
> php spark restful:publish
> php spark settings:publish
> php spark cronjob:publish
> php spark jwt:publish
此命令将配置文件复制到您的应用命名空间。然后您可以调整它以满足您的需求。默认文件将位于 app/Config/。
> php spark migrate -all
此命令将在您的数据库中创建 restful 表。
用法
<?php namespace App\Controllers; use CodeIgniter\RESTful\ResourceController; use Daycry\RestFul\RestFul; use Daycry\RestFul\Traits\Authenticable; class Center extends ResourceController { use RestFul; use Authenticable; public function index() { return $this->respond( $this->content ); } } If you need to validate the data, you can call `validation` method passing the string rules, array of data and Validation Config file if you need. By default load `App\Config\Validation.php` rules. For Example: `app/Config/Validation.php` or if rules are in custom namespace `app/Modules/Example/Config/Validation.php` ```php public $requiredLogin = [ 'username' => 'required', 'password' => 'required' ];
<?php namespace App\Controllers; use CodeIgniter\RESTful\ResourceController; use Daycry\RestFul\RestFul; use Daycry\RestFul\Traits\Authenticable; use Daycry\RestFul\Traits\Validation; class Center extends ResourceController { use RestFul; use Authenticable; use Validation; public function index() { $this->validation( 'requiredLogin', $this->content ); return $this->respond( $this->content ); } }
$this->content 包含请求中的正文内容。 $this->args 包含所有参数,get、post、headers 等...
如果您想使用对象 $this->request 来获取这些参数,例如,$this->request->getPost()
<?php namespace App\Controllers; use CodeIgniter\RESTful\ResourceController; use Daycry\RestFul\RestFul; use Daycry\RestFul\Traits\Authenticable; use Daycry\RestFul\Traits\Validation; class Center extends ResourceController { use RestFul; use Authenticable; use Validation; public function index() { $this->validation( 'requiredLogin', $this->content, config( Example\\Validation ), true, true ); return $this->respond( $this->content ); } }
验证函数参数
# 访问过滤器
您可以通过访问过滤器设置作用域来限制请求
<?php namespace App\Config; $routes->group('group', ['namespace' => 'App\Controllers'], static function ($routes) { $routes->post('search/(:segment)', 'Search::$1', [ 'filter' => 'access:example.read' ]); $routes->post('auth/(:segment)', 'Auth::$1', [ 'filter' => 'access:example.auth' ]); });
用户模型类
默认情况下,您可以通过 '\Daycry\RestFul\Models\UserModel' 模型将用户与键关联起来,但您可以自定义扩展此类。
示例
<?php namespace App\Models; use CodeIgniter\Validation\ValidationInterface; use CodeIgniter\Database\ConnectionInterface; use Daycry\RestFul\Models\UserModel as RestFulUserModel class UserModel extends RestFulUserModel { protected $allowedFields = [ 'username', 'scopes' ]; public function __construct(?ConnectionInterface &$db = null, ?ValidationInterface $validation = null) { parent::__construct($db, $validation); } }
如果您自定义了用户类,您必须修改默认配置
示例
<?php public string $userProvider = \Daycry\RestFul\Models\UserModel::class;
异常 & 阻止无效尝试
如果您想使用一些自定义异常将其用作失败的请求尝试并允许阻止该 IP,您必须创建静态属性 authorized。
如果 authorized 为 false,则系统将增加该 IP 的失败尝试次数。例如
<?php namespace App\Exceptions; use Daycry\RestFul\Exceptions\RuntimeException; class CustomException extends RuntimeException { 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
您可以使用 ws_endpoints 表独立地自定义请求。
您可以使用命令自动填充 ws_controllers。
<?php
php spark restful:discover
此命令在您指定的命名空间或多个命名空间中搜索类。您可以在 RestFul.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 array $namespaceScope = ['\App\Controllers', '\Api\Controllers'];
或者,通过编辑 CronJob.php 配置文件来创建 cronjob 任务,如下所示。
<?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('restful:discover')->named('discoverRestful')->daily(); or $schedule->command('restful:discover')->named('discoverRestful')->daily('11:30 am'); }
有关 cronjob 的更多信息: https://github.com/daycry/cronjob
响应
默认响应为 json,但您可以在头部将其更改为 xml。
Accept: application/json
或者
Accept: application/xml
输入体
请愿书的体默认为 json,但您可以更改它。
Content-Type: application/json
或者
Content-Type: application/xml
API 令牌
您可以在头部、GET 或 POST 变量中发送 api rest token,如下所示。
X-API-KEY: TOKEN
http://example.com?X-API-KEY=key
语言
您可以这样发送 language。
Accept-Language: en