mardira-framework icon indicating copy to clipboard operation
mardira-framework copied to clipboard

Mardira Framework PHP for Back-End Developer

Mardira Logo

Mardira Framework is a PHP framework Model Controller Based for building web applications and APIs. It is designed to be simple, and fast.

Total Downloads Total Stars Total Forks Version License

Table of Contents

  • Requirements
  • Structure Folders
  • Installation
  • Usage
    • Start Server
    • Create .env
    • Create Controller
    • Create Model
    • Create Route
    • Create Migration
    • Run Migration
    • Refresh Migration
    • Refresh Migration With Seed
    • Create Seeder
    • Run Seeder
    • Run Seeder Specific
    • Create Authetication
    • Refresh Authetication
    • Update Framework Version
    • Controller
    • Model
    • Migration
    • Seeder
    • Middleware
    • Route
      • Route Group
    • Query Builder
      • Select
      • Where
      • Or Where
      • Where In
      • Where Not In
      • Where Null
      • Where Not Null
      • Order By
      • Group By
      • Join
      • Insert
      • Update
      • Delete
      • Count

Requirements

  • PHP = 7.4
  • MySQL >= 5.7.8
  • Apache >= 2.4.41
  • Composer >= 2.0.9

Structure Folders

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

Installation

Setup

You can create a new project using composer

composer create-project mardira/mardira-framework <your_project_name>

or you can clone this project

Clone

  • Clone this repo to your local machine using `git clone
  git clone https://github.com/Bootcamp-STMIK-Mardira-Indonesia/mardira-framework.git

Then, install the dependencies using composer

composer install

or

composer update

Usage

Start Server

php mardira serve

or

php mardira serve --port=<your_port>

Create .env

You can create .env file using command

php mardira make:env

Create Controller

php mardira make:controller ControllerName

Create Model

php mardira make:model ModelName

Create Route

php mardira make:route route_name --controller=ControllerName

Create Migration


php mardira make:migration create_table_table_name

Run Migration

If database not exist, will automatically create database from .env

php mardira migrate

Refresh Migration

php mardira migrate:refresh

Refresh Migration With Seed

php mardira migrate:refresh --seed

Create Seeder

php mardira make:seeder SeederName

Run Seeder

php mardira db:seed

Run Seeder Specific

php mardira db:seed --class=SeederName

Create Authetication

php mardira make:auth

Refresh Authetication

php mardira make:auth --refresh

Update Framework Version

php mardira update

Controller

Create controller use php mardira make:controller ControllerName, here is example controller

<?php

namespace App\Controllers;

use App\Core\Controller;

class HomeController extends Controller
{
    public function index()
    {
        $this->response(200,[
            'message' => 'Hello World'
        ]);
    }
}

to use controller, you can add route in App/Routes/Api.php

<?php

use App\Core\Route;
use App\Controllers\HomeController;

Route::get('/home', [HomeController::class, 'index']);

Response

You can use response in controller

$this->response(200,[
    'message' => 'Hello World'
]);

return json expected

{
  "message": "Hello World"
}

another response example 409

$this->response->json(409,[
    'message' => 'Conflict'
]);

Model

Create model use php mardira make:model ModelName, here is example model

<?php

namespace App\Models;

use App\Core\Model;

class User extends Model
{
    protected $table = 'users';
    protected $primaryKey = 'id';
}

to use model, you can add model in 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
        ]);
    }
}

Migration

Create migration use php mardira make:migration create_table_table_name, here is example migration

<?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

Create seeder use php mardira make:seeder SeederName, here is example 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);
    }
}

Middleware

Create middleware use php mardira make:middleware MiddlewareName, here is example middleware

<?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']);
    }
}

to use middleware, you can add middleware in route


Router::get('/schedules', [ScheduleController::class, 'index'], [AuthMiddleware::class]);

Routing

You can add route in App/Routes/Api.php


<?php

use App\Core\Route;

Router::get('/home', [HomeController::class, 'index']);

Route Group

You can add route group in App/Routes/Api.php


<?php

use App\Core\Route;


Router::controller(ProductController::class)->group(function () {
    Router::post('/products/store', 'store');
});

Query Builder


use App\Core\QueryBuilder as DB;

Select

DB::table('users')->select('name', 'email')->get();

Where


// 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();

Or Where



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();

Where In


DB::table('users')->whereIn('id', [1,2,3])->get();

DB::table('users')->whereNotIn('id', [1,2,3])->get();

Where Not In


DB::table('users')->whereNotIn('id', [1,2,3])->get();

Where Null


DB::table('users')->whereNull('id')->get();

Where Not Null

DB::table('users')->whereNotNull('id')->get();

Order By


DB::table('users')->orderBy('id', 'desc')->get();

DB::table('users')->orderBy('id', 'asc')->get();

Join Table


DB::table('users')
    ->join('roles', 'users.role_id', '=', 'roles.id')
    ->select('users.*', 'roles.name as role_name')
    ->get();

Group By


DB::table('users')
    ->groupBy('role_id')
    ->get();

Insert


DB::table('users')->insert([
    'name' => 'user',
    'email' => '[email protected]',
    'password' => password_hash('password', PASSWORD_DEFAULT),
]);

Update


DB::table('users')->where('id', 1)->update([
    'name' => 'user',
    'email' => '[email protected]',
]);

Delete


DB::table('users')->where('id', 1)->delete();

Count


DB::table('users')->count();

Support

Reach out to me at one of the following places!