Mardiraフレームワークは、WebアプリケーションとAPIの構築に基づいたPHPフレームワークモデルコントローラーです。シンプルで速いように設計されています。
mardira-framework
├── App
│ ├── Controllers
│ │ ├── AuthController.php
│ ├── Core
│ │ ├── Commands
│ ├── Database
│ │ ├── Migrations
│ │ │ ├── 2023_01_31_xxxxxx_create_table_users.php
│ │ │ ├── 2023_01_31_xxxxxx_create_table_roles.php
│ │ ├── Seeders
│ │ │ ├── GlobalSeeder.php
│ ├── Helpers
│ ├── Middleware
│ ├── Models
│ ├── Packages
│ ├── Routes
│ │ ├── Api.phpComposerを使用して新しいプロジェクトを作成できます
composer create-project mardira/mardira-framework < your_project_name >または、このプロジェクトをクローンすることができます
git clone https://github.com/Bootcamp-STMIK-Mardira-Indonesia/mardira-framework.git次に、Composerを使用して依存関係をインストールします
composer installまたは
composer updatephp mardira serveまたは
php mardira serve --port= < your_port >Commandを使用して.ENVファイルを作成できます
php mardira make:envphp mardira make:controller ControllerNamephp mardira make:model ModelNamephp mardira make:route route_name --controller=ControllerNamephp mardira make:migration create_table_table_nameデータベースが存在しない場合、.envからデータベースを自動的に作成します
php mardira migratephp mardira migrate:refreshphp mardira migrate:refresh --seedphp mardira make:seeder SeederNamephp mardira db:seedphp mardira db:seed --class=SeederNamephp mardira make:authphp mardira make:auth --refreshphp mardira updateコントローラーの作成
php mardira make:controller ControllerName使用してください。
<?php
namespace App Controllers ;
use App Core Controller ;
class HomeController extends Controller
{
public function index ()
{
$ this -> response ( 200 ,[
' message ' => ' Hello World '
]);
}
}コントローラーを使用するには、
App/Routes/Api.phpにルートを追加できます
<?php
use App Core Route ;
use App Controllers HomeController ;
Route:: get ( ' /home ' , [HomeController::class, ' index ' ]);コントローラーで応答を使用できます
$ this -> response ( 200 ,[
' message ' => ' Hello World '
]);JSONを返すことが期待されます
{
"message" : " Hello World "
}別の応答例409
$ this -> response -> json ( 409 ,[
' message ' => ' Conflict '
]);モデルの作成
php mardira make:model ModelName、これがモデルの例です
<?php
namespace App Models ;
use App Core Model ;
class User extends Model
{
protected $ table = ' users ' ;
protected $ primaryKey = ' id ' ;
}モデルを使用するには、
App/Controllers/ControllerName.phpにモデルを追加できます
<?php
namespace App Controllers ;
use App Core Controller ;
use App Models User ;
class HomeController extends Controller
{
public function index ()
{
$ user = User:: all ();
$ this -> response ( 200 ,[
' message ' => ' Hello World ' ,
' data ' => $ user
]);
}
}移行の
php mardira make:migration create_table_table_name
<?php
namespace App Database Migrations ;
use App Core Migration ;
return new class extends Migration
{
public function up ()
{
$ this -> schema -> create ( ' users ' , function ( $ table ) {
$ table -> increment ( ' id ' );
$ table -> string ( ' name ' , 50 );
$ table -> string ( ' email ' , 50 )-> unique ();
$ table -> string ( ' password ' , 64 );
$ table -> timestamps ();
});
}
public function down ()
{
$ this -> schema -> dropIfExists ( ' users ' );
}
}シーダーを作成する
php mardira make:seeder SeederName、ここにSeederの例があります
<?php
namespace App Database Seeders ;
use App Core Seeder ;
use App Core QueryBuilder as DB ;
class UserSeeder extends Seeder
{
public function run ()
{
$ data = [
[
' name ' => ' Administrator ' ,
' username ' => ' admin ' ,
' email ' => ' [email protected] ' ,
' password ' => password_hash ( ' password ' , PASSWORD_DEFAULT ),
' role_id ' => 1 ,
],
[
' name ' => ' User ' ,
' username ' => ' user ' ,
' email ' => ' [email protected] ' ,
' password ' => password_hash ( ' password ' , PASSWORD_DEFAULT ),
' role_id ' => 2 ,
]
];
DB :: table ( ' users ' )-> insert ( $ data );
}
}ミドルウェアの作成
php mardira make:middleware MiddlewareName、これがミドルウェアの例です
<?php
namespace App Middleware ;
use App Core Middleware ;
use App Core Auth ;
class AuthMiddleware extends Middleware
{
public function handle ()
{
if (Auth:: check ()) {
return $ next ();
}
return $ this -> response ( 401 , [ ' message ' => ' Unauthorized ' ]);
}
}ミドルウェアを使用するには、ルートにミドルウェアを追加できます
Router:: get ( ' /schedules ' , [ScheduleController::class, ' index ' ], [AuthMiddleware::class]);
App/Routes/Api.phpにルートを追加できます
<?php
use App Core Route ;
Router:: get ( ' /home ' , [HomeController::class, ' index ' ]);
App/Routes/Api.phpにルートグループを追加できます
<?php
use App Core Route ;
Router:: controller (ProductController::class)-> group ( function () {
Router:: post ( ' /products/store ' , ' store ' );
}); use App Core QueryBuilder as DB ; DB :: table ( ' users ' )-> select ( ' name ' , ' email ' )-> get (); // equal
DB :: table ( ' users ' )-> where ( ' id ' , 1 )-> get ();
DB :: table ( ' users ' )-> where ( ' id ' , 1 , ' > ' )-> get ();
DB :: table ( ' users ' )-> where ( ' id ' , 1 , ' < ' )-> get ();
DB :: table ( ' users ' )-> where ( ' id ' , 1 , ' >= ' )-> get ();
DB :: table ( ' users ' )-> where ( ' id ' , 1 , ' <= ' )-> get ();
DB :: table ( ' users ' )-> where ( ' id ' , 1 , ' != ' )-> get ();
DB :: table ( ' users ' )-> where ( ' id ' , 1 , ' <> ' )-> get ();
// like
DB :: table ( ' users ' )-> where ( ' name ' , ' admin ' , ' like ' )-> get ();
DB :: table ( ' users ' )-> where ( ' name ' , ' admin ' , ' not like ' )-> get (); DB :: table ( ' users ' )-> orWhere ( ' id ' , 1 )-> get ();
DB :: table ( ' users ' )-> orWhere ( ' id ' , 1 , ' > ' )-> get ();
DB :: table ( ' users ' )-> orWhere ( ' id ' , 1 , ' < ' )-> get ();
DB :: table ( ' users ' )-> orWhere ( ' id ' , 1 , ' >= ' )-> get ();
DB :: table ( ' users ' )-> orWhere ( ' id ' , 1 , ' <= ' )-> get ();
DB :: table ( ' users ' )-> orWhere ( ' id ' , 1 , ' != ' )-> get ();
DB :: table ( ' users ' )-> orWhere ( ' id ' , 1 , ' <> ' )-> get (); DB :: table ( ' users ' )-> whereIn ( ' id ' , [ 1 , 2 , 3 ])-> get ();
DB :: table ( ' users ' )-> whereNotIn ( ' id ' , [ 1 , 2 , 3 ])-> get (); DB :: table ( ' users ' )-> whereNotIn ( ' id ' , [ 1 , 2 , 3 ])-> get (); DB :: table ( ' users ' )-> whereNull ( ' id ' )-> get (); DB :: table ( ' users ' )-> whereNotNull ( ' id ' )-> get (); DB :: table ( ' users ' )-> orderBy ( ' id ' , ' desc ' )-> get ();
DB :: table ( ' users ' )-> orderBy ( ' id ' , ' asc ' )-> get (); DB :: table ( ' users ' )
-> join ( ' roles ' , ' users.role_id ' , ' = ' , ' roles.id ' )
-> select ( ' users.* ' , ' roles.name as role_name ' )
-> get (); DB :: table ( ' users ' )
-> groupBy ( ' role_id ' )
-> get (); DB :: table ( ' users ' )-> insert ([
' name ' => ' user ' ,
' email ' => ' [email protected] ' ,
' password ' => password_hash ( ' password ' , PASSWORD_DEFAULT ),
]); DB :: table ( ' users ' )-> where ( ' id ' , 1 )-> update ([
' name ' => ' user ' ,
' email ' => ' [email protected] ' ,
]); DB :: table ( ' users ' )-> where ( ' id ' , 1 )-> delete (); DB :: table ( ' users ' )-> count ();次の場所のいずれかで私に連絡してください!
demostmikmi.comのウェブサイト