PHP code example of mevdschee / php-crud-api

1. Go to this page and download the library: Download mevdschee/php-crud-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mevdschee / php-crud-api example snippets


GET /records/categories?order=name,desc
GET /records/categories?order=id,desc&order=name

GET /records/categories?order=id,desc&size=1

GET /records/categories?order=id&page=1
GET /records/categories?order=id&page=1,50

...,
'middlewares' => 'cors, jwtAuth, authorization',
        'jwtAuth.secrets' => "ce5ced6e40dcd1eff407048867b1ed1e706686a0:-----BEGIN CERTIFICATE-----\nMIIDHDCCAgSgAwIBAgIIExun9bJSK1wwDQYJKoZIhvcNAQEFBQAwMTEvMC0GA1UE\nAxMmc2VjdXJldG9rZW4uc3lzdGVtLmdzZXJ2aWNlYWNjb3VudC5jb20wHhcNMTkx\nMjIyMjEyMTA3WhcNMjAwMTA4MDkzNjA3WjAxMS8wLQYDVQQDEyZzZWN1cmV0b2tl\nbi5zeXN0ZW0uZ3NlcnZpY2VhY2NvdW50LmNvbTCCASIwDQYJKoZIhvcNAQEBBQAD\nggEPADCCAQoCggEBAKsvVDUwXeYQtySNvyI1/tZAk0sj7Zx4/1+YLUomwlK6vmEd\nyl2IXOYOj3VR7FBA24A9//nnrp+mV8YOYEOdaWX7PQo0PIPFPqdA0r7CqBUWHPfQ\n1WVHVRQY3G0c7upM97UfMes9xOrMqyvecMRk1e5S6eT12Zh2og7yiVs8gP83M1EB\nGqseUaltaadjyT35w5B0Ny0/7NdLYiv2G6Z0S821SxvSo1/wfmilnBBKYYluP0PA\n9NPznWFP6uXnX7gKxyJT9//cYVxTO6+b1TT13Yvrpm1a4EuCOhLrZH6ErHQTccAM\nhAx8mdNtbROsp0dlPKrSfqO82uFz45RXZYmSeP0CAwEAAaM4MDYwDAYDVR0TAQH/\nBAIwADAOBgNVHQ8BAf8EBAMCB4AwFgYDVR0lAQH/BAwwCgYIKwYBBQUHAwIwDQYJ\nKoZIhvcNAQEFBQADggEBACNsJ5m00gdTvD6j6ahURsGrNZ0VJ0YREVQ5U2Jtubr8\nn2fuhMxkB8147ISzfi6wZR+yNwPGjlr8JkAHAC0i+Nam9SqRyfZLqsm+tHdgFT8h\npa+R/FoGrrLzxJNRiv0Trip8hZjgz3PClz6KxBQzqL+rfGV2MbwTXuBoEvLU1mYA\no3/UboJT7cNGjZ8nHXeoKMsec1/H55lUdconbTm5iMU1sTDf+3StGYzTwC+H6yc2\nY3zIq3/cQUCrETkALrqzyCnLjRrLYZu36ITOaKUbtmZhwrP99i2f+H4Ab2i8jeMu\nk61HD29mROYjl95Mko2BxL+76To7+pmn73U9auT+xfA=\n-----END CERTIFICATE-----\n",
        'jwtAuth.audiences' => 'your-project-id',
        'cors.allowedOrigins' => '*',
        'cors.allowHeaders' => 'X-Authorization'

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Tqdev\PhpCrudApi\Cache\Cache;
use Tqdev\PhpCrudApi\Column\ReflectionService;
use Tqdev\PhpCrudApi\Config\CustomSettings;
use Tqdev\PhpCrudApi\Controller\Responder;
use Tqdev\PhpCrudApi\Database\GenericDB;
use Tqdev\PhpCrudApi\Middleware\Router\Router;

class MyHelloController {

    private $responder;
    private $greeting;

    public function __construct(Router $router, Responder $responder, GenericDB $db, ReflectionService $reflection, Cache $cache, CustomSettings $settings)
    {
        $router->register('GET', '/hello', array($this, 'getHello'));
        $this->responder = $responder;
        $this->greeting = $settings->get('greeting', 'Hello World!');
    }

    public function getHello(ServerRequestInterface $request): ResponseInterface
    {
        return $this->responder->success(['message' => $this->greeting]);
    }
}

PHP_CRUD_API_MY_HELLO_CONTROLLER_GREETING="Hello Config!"

use Tqdev\PhpCrudApi\Column\ReflectionService;
use Tqdev\PhpCrudApi\Config\CustomSettings;
use Tqdev\PhpCrudApi\OpenApi\OpenApiDefinition;
use Tqdev\PhpCrudApi\OpenApi\OpenApiMiddlewares;

class MyHelloOpenApiBuilder {

    private $openapi;
    private $middlewares;
    private $description;

    public function __construct(OpenApiDefinition $openapi, ReflectionService $reflection, OpenApiMiddlewares $middlewares, CustomSettings $settings)
    {
        $this->openapi = $openapi;
        $this->middlewares = $middlewares;
        $this->description = $settings->get('description', 'Say hello');
    }

    public function build() /*: void*/
    {
        $path = 'paths|/hello|get';
        foreach ($this->middlewares->getCommonParameters('get') as $parameter) {
            $this->openapi->set("$path|parameters||\$ref", "#/components/parameters/$parameter");
        }
        $this->openapi->set("$path|tags|", 'hello');
        $this->openapi->set("$path|operationId", 'get_hello');
        $this->openapi->set("$path|description", $this->description);
        $this->openapi->set("$path|responses|200|description", 'the greeting');
        $this->openapi->set("$path|responses|200|content|application/json|schema|type", 'object');
        $this->openapi->set("$path|responses|200|content|application/json|schema|properties|message|type", 'string');
        $statusCodes = array_merge($this->middlewares->getStatusCodes(), [500]);
        sort($statusCodes);
        foreach ($statusCodes as $statusCode) {
            $this->openapi->set("$path|responses|$statusCode|\$ref", "#/components/responses/error-$statusCode");
        }
        $this->openapi->set('tags|', ['name' => 'hello', 'description' => 'hello operations']);
    }
}

$this->openapi->set('components|securitySchemes|oauth2|type', 'oauth2');
$this->openapi->set('components|securitySchemes|oauth2|flows|implicit|authorizationUrl', 'https://example.com/authorize');
$this->openapi->set('components|securitySchemes|oauth2|flows|implicit|scopes', new \stdClass());
$this->openapi->set('security||oauth2', []);

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;
    server_name server_domain_or_IP;

    location / {
        try_files $uri $uri/ /api.php?$args;  
    }

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        try_files $fastcgi_script_name =404;
        set $path_info $fastcgi_path_info;
        fastcgi_param PATH_INFO $path_info;
        fastcgi_index index.php;