mardira framework
v1.1.6
Mardira Framework是用於構建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.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 ' );
}
}創建播種機使用
php mardira make:seeder SeederName,這是示例播種機
<?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