daycry / restserver
Rest Server for Codeigniter 4
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 Server
Rest Server with Doctrine for Codeigniter 4
Version 7
This version breaks compatibility with previous versions, since it contains new automated features, restructuring of the database with new foreign keys, so if you update, check that the names of the tables are in the plural.
Installation via composer
Use the package with composer install
> composer require daycry/restserver
Manual installation
Download this repo and then enable it by editing app/Config/Autoload.php and adding the Daycry\RestServer namespace to the $psr4 array. For example, if you copied it into app/ThirdParty:
$psr4 = [ 'Config' => APPPATH . 'Config', APP_NAMESPACE => APPPATH, 'App' => APPPATH, 'Daycry\RestServer' => APPPATH .'ThirdParty/restserver/src', ];
Configuration
Run command:
> php spark restserver:publish
> php spark settings:publish
> php spark cronjob:publish
> php spark jwt:publish
This command will copy a config file to your app namespace.
Then you can adjust it to your needs. By default file will be present in app/Config/RestServer.php
.
> php spark migrate -all
This command create rest server tables in your database.
If you want load and Example Seed you can use this command.
> php spark db:seed Daycry\RestServer\Database\Seeds\ExampleSeeder
More information about install doctrine: https://github.com/daycry/doctrine
Usage Loading Library
<?php namespace App\Controllers; class Center extends \Daycry\RestServer\RestServer { public function index() { return $this->respond( $this->content ); } }
If you want change attributes before controller call:
<?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 ); } }
If you need to validate the data, you can call validation
method passing the string rules and Validation Config file y you need.
For Example: app/Config/Validation.php
or if rules are in custom namespace 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 contains a body content in the request. $this->args contains all params, get, post, headers,... but you can use the object $this->request for get this params if you want.
<?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 ); } }
Validation function parameters
Fiels | Desctiption |
---|---|
rule | Name of rule |
namespace | Namespace that contains the rule |
getShared | true or false |
filter | If you want to limit the content of the body, exclusively to the parameters of the rule. |
User Model Class
By default you can associate users with keys via the '\Daycry\RestServer\Models\UserModel' model, but you can customize it by creating an existing class of '\Daycry\RestServer\Libraries\User\UserAbstract'.
Example:
<?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; }
If you customize the user class, you have to modify the default configuration
Example:
<?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
Exceptions & block Invalid Attempts
If you want to use some custom exception to use it as a failed request attempt and allow the blocking of that IP, you have to create the static attribute authorized.
If authorized is false the system increases by 1 the failed attempts by that IP. Example:
<?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
You can customize the requests independently using the petition
table.
Fiels | Value | Options | Desctiption |
---|---|---|---|
namespace_id |
This field contains the identifier of the ws_namespaces table, this table stores the namespaces of the classes, for example \App\Controllers\Auth |
||
method |
login |
Use this field to configure the method of controller | |
http |
post |
get ,post ,put ,patch , options |
Use this field to configure the method of controller |
auth |
bearer |
false ,basic ,digest ,bearer |
Use this field to configure the autentication method |
log |
null |
null ,1 ,0 |
Use this field if you want log the petition |
limit |
null |
null ,1 ,15 |
Use this field if you want to set a request limit, this value must be an integer |
time |
null |
null ,1 ,15 |
This field is used to know how often the request limit is reset ( Time in seconds Example: 3600 -> In this case you can do {limit} request in 3600 seconds) |
level |
null |
null ,1 ,10 |
Use this field to indicate the level of permissions in the request, if the token has level 1 and the request has level 3, you will not be able to make the request |
You can fill the ws_namespaces automatically with a command.
<?php
php spark restserver:discover
This command search class in the namespace o namespaces that you want. You can set this namespaces in the RestServer.php config file.
<?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'];
Or creating a cronjob tasks editing CronJob.php config file like this.
<?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'); }
More information about cronjob: https://github.com/daycry/cronjob
RESPONSE
The default response is in json
but you can change to xml
in the headers.
Accept: application/json
or
Accept: application/xml
or you can set format in GET variable
http://example.com?format=json
http://example.com?format=xml
INPUT BODY
The body of petition is json
by default, but you can change it.
Content-Type: application/json
or
Content-Type: application/xml
API TOKEN
You can sent api rest token
in headers, GET or POST variable like this.
X-API-KEY: TOKEN
http://example.com?X-API-KEY=key
LANGUAGE
You can sent language
like this.
Accept-Language: en