devvime/kiichi-php-engine

Simple Package PHP for developing web API`s with Kiichi PHP.

1.0.8 2024-03-16 06:56 UTC

This package is auto-updated.

Last update: 2024-04-16 07:13:13 UTC


README

Simple Package PHP for developing web API`s with Kiichi PHP.

Specifications and Dependencies

install package

Run the composer command in project root:

composer require devvime/kiichi-php-engine

Configuration

<?php

require_once(__DIR__.'vendor/autoload.php');

use Devvime\KiichiPhpEngine\Application;

$app = new Application();

Creating routes

Route and Function

$app->get('/', function($req, $res) {
    $res->json(['title'=>'Simple CRUD PHP']);
});

Route and Class

Folder structure for using classes

├── App
|  ├── Controllers
│  |  └── ProductController.php
|  |── Models
│  |  └── Product.php
|  |── Middlewares
│  |  └── ExempleMiddleware.php

Class name must contain the word Controller, for example: UserController.php

$app->get('/:id', 'UserController@find');

Group of routes and parameters in URL

$app->group('/hello', function() use($app) {
    $app->get('/:name', function($req, $res) {
        $res->render('html-file-name', [
            "name"=>$req->params->name            
        ]);
    });
});

$app->group('/user', function() use($app) {
    $app->get('', 'UserController@index');
    $app->get('/:id', 'UserController@find');
    $app->post('', 'UserController@store');
    $app->put('/:id', 'UserController@update');
    $app->delete('/:id', 'UserController@destroy');
});

Route and Middleware

Middleware Class name must contain the word Middleware, for example: AuthMiddleware.php

// Middleware in  Function
$app->get('/:id', 'UserController@find', function() {
    // Middleware code...
});

// Middleware in Class
$app->get('/:id', 'UserController@find', 'UserMiddleware@verifyAuthToken');

// Middleware Function in Route Group

$app->group('/user', function() use($app) {
    $app->get('', 'UserController@index');
    $app->get('/:id', 'UserController@find');
    $app->post('', 'UserController@store');
    $app->put('/:id', 'UserController@update');
    $app->delete('/:id', 'UserController@destroy');
}, function() {
    // Middleware code...
});

// Middleware Class in Route Group

$app->group('/user', function() use($app) {
    $app->get('', 'UserController@index');
    $app->get('/:id', 'UserController@find');
    $app->post('', 'UserController@store');
    $app->put('/:id', 'UserController@update');
    $app->delete('/:id', 'UserController@destroy');
}, 'AuthMiddleware@verifyToken');

Request data

Request data in URL Query ex: http://api.server.com/user?name=steve

$app->post('/user', function($req, $res) {
    $name = $req->query->name;
});

Request post data JSON

$app->post('/user', function($req, $res) {
    $name = $req->body->name;
    $email = $req->body->email;
});

Request params in URL

$app->put('/:id', function($req, $res) {
    $id = $req->params->id;
});

Start routes

$app->run();

Render HTML file

To render an HTML file just use $res->render('file-name'); no need to add .html in file name

$app->get('/user', function($req, $res) use($app) {
    $res->render('html-file-name');
});

To render an HTML file by sending an array of data use $res->render('file-name');

$app->get('/user', function($req, $res) use($app) {
    $res->render('html-file-name', [
        "name"=>$user->name,
        "email"=>$user->email,
        "product"=>$productArray
    ]);
});

To receive the data sent to the HTML file use {{ key }} or {{ key.object.name }}

<div class="card">
    <h4>{{ name }}</h4>
    <p>{{ email }}</p>
    <hr/>
    <p>{{ product.title }}</p>
    <p>{{ product.description }}</p>
    <p>{{ product.price }}</p>
</div>

For more details, see the documentation at RainTPL 3