mochrira/selvi-framework
最新稳定版本:2.3.1
Composer 安装命令:
composer require mochrira/selvi-framework
包简介
Super fast PHP Framework for building API
README 文档
README
⚡ Super fast PHP Framework for building API
Quick Start
- Get this framework via composer on your project directory (inside www folder if you are using Apache)
$ composer require mochrira/selvi-framework
- Create
app/Controllersfolder inside your project directory - Create file
HomeController.phpinsideapp/Controllerswith this content
<?php
namespace App\Controllers;
use Selvi\Controller;
class HomeController extends Controller {
function index() {
return response('Welcome to Selvi Framework');
}
}
- Create index.php
<?php
require('vendor/autoload.php');
Selvi\Routing\Route::get('/', 'App\\Controllers\\HomeController@index');
Selvi\Framework::run();
- Create
.htaccessfile
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
- Edit your composer.json
{
...
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
...
}
- Run
composer updateto update your composer autoload - Done. Open
http://localhost/your-projecton your browser to test it
Database Configuration
- Buka http://localhost:1000, buat database baru dengan nama
example_app - Buat file
app/Config/database.phpdengan konten sebagai berikut:
<?php
use Selvi\Database\Manager;
use Selvi\Database\Migration;
use Selvi\Cli;
Manager::add('main', [
'driver' => 'mysql',
'host' => 'mariadb.database',
'username' => 'root',
'password' => 'changeme',
'database' => 'example_app'
]);
Migration::addMigrations('main', [ BASEPATH.'/app/Migrations' ]);
Cli::register('migrate', Migration::class);
- Modifikasi file
index.phpdengan menambahkan baris berikut setelah require vendor
<?php
require __DIR__.'/vendor/autoload.php';
define('BASEPATH', __DIR__); // tambahkan baris ini
require __DIR__.'/app/Config/database.php'; // tambahkan baris ini
Selvi\Routing\Route::get('/', 'App\\Controllers\\HomeController@index');
\Selvi\Framework::run();
Migration
- Buat folder
app/Migrations - Buat file
20250702_01_init.php(format : YYYYMMDD_{index}_{konteks}.php), dengan konten sebagai berikut :
<?php
return function ($schema, $direction) {
if($direction == 'up') :
$schema->create('kontak', [
'idKontak' => 'INT(11) PRIMARY KEY AUTO_INCREMENT',
'nmKontak' => 'VARCHAR(150)',
'nomor' => 'VARCHAR(50)'
]);
endif;
if($direction == 'down') :
$schema->drop('kontak');
endif;
};
- Jalankan migrasi dengan perintah
php index.php migrate main up
Swagger UI
- Buat file
www/specs/index.htmldengan konten sebagai berikut :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="SwaggerUI" />
<title>SwaggerUI</title>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@4.5.0/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@4.5.0/swagger-ui-bundle.js" crossorigin></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: './index.yaml',
dom_id: '#swagger-ui',
persistAuthorization: true
});
};
</script>
</body>
</html>
- Buat file
www/specs/index.yamldengan konten sebagai berikut :
openapi: '3.0.2'
info:
title: Kontak API
version: '1.0'
servers:
- url: http://localhost:8080
description: Development
paths:
/kontak:
get:
summary: Mengambil semua data kontak
tags: [Kontak]
parameters:
- in: query
name: offset
schema:
type: integer
- in: query
name: limit
schema:
type: integer
- in: query
name: search
schema:
type: string
responses:
'200':
description: Semua object kontak dalam database
post:
summary: Menambahkan kontak baru
tags: [Kontak]
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
nmKontak:
type: string
nomor:
type: string
example:
nmKontak: Moch. Rizal
nomor: 82143255597
responses:
'201':
description: Berhasil menambahkan kontak
/kontak/{idKontak}:
get:
summary: Mengambil kontak berdasarkan ID
tags: [Kontak]
parameters:
- in: path
required: true
name: idKontak
schema:
type: integer
responses:
'200':
description: Object kontak terpilih
patch:
summary: Mengubah kontak berdasarkan ID
tags: [Kontak]
parameters:
- in: path
required: true
name: idKontak
schema:
type: integer
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
nmKontak:
type: string
nomor:
type: string
example:
nmKontak: Moch. Rizal Rachmdani
nomor: 82143255597
responses:
'204':
description: Berhasil merubah kontak
delete:
summary: Menghapus kontak berdasarkan ID
tags: [Kontak]
parameters:
- in: path
required: true
name: idKontak
schema:
type: integer
responses:
'204' :
description: Kontak berhasil dihapus
统计信息
- 总下载量: 1.28k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 1
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-07-14