Mardira Framework เป็นตัวควบคุมโมเดล PHP Framework สำหรับการสร้างเว็บแอปพลิเคชันและ API มันถูกออกแบบมาให้เรียบง่ายและรวดเร็ว
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.phpคุณสามารถสร้างโครงการใหม่โดยใช้นักแต่งเพลง
composer create-project mardira/mardira-framework < your_project_name >หรือคุณสามารถโคลนโครงการนี้ได้
git clone https://github.com/Bootcamp-STMIK-Mardira-Indonesia/mardira-framework.gitจากนั้นติดตั้งการอ้างอิงโดยใช้นักแต่งเพลง
composer installหรือ
composer updatephp mardira serveหรือ
php mardira serve --port= < your_port >คุณสามารถสร้างไฟล์. 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 ' );
}
}สร้าง seeder ใช้
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