PHP code example of jahurul1 / ji-framework

1. Go to this page and download the library: Download jahurul1/ji-framework library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jahurul1 / ji-framework example snippets



$app = new JiFramework\Core\App\App();


JiFramework\Core\App\App;

$app = new App();

// Query the database
$users = $app->db->table('users')->where('active', '=', 1)->get();

// Log something
$app->logger->info('Page loaded', ['user_count' => count($users)]);

// Validate input
$v = $app->validator->make($_POST, [
    'email' => '

// jiconfig.php
return [
    'app_mode' => 'production',
    'timezone' => 'Asia/Dhaka',

    'database' => [
        'host'     => 'localhost',
        'database' => 'my_db',
        'username' => 'root',
        'password' => 'secret',
    ],

    'log_enabled'        => true,
    'rate_limit_enabled' => true,
    'router_enabled'     => true,
];

$app->router->get('/', 'pages/home.php');
$app->router->get('/users/{id}', 'pages/user.php');  // $id available in file

$app->router->get('/ping', function () {
    header('Content-Type: application/json');
    echo json_encode(['status' => 'ok']);
});

$app->router->post('/login', 'pages/auth/login.php');

$app->router->group(['prefix' => '/admin'], function ($r) {
    $r->get('/dashboard', 'pages/admin/dashboard.php');
    $r->get('/users',     'pages/admin/users.php');
});

$app->router->dispatch();

// Select with conditions
$users = $app->db->table('users')
    ->where('active', '=', 1)
    ->where('age', '>', 18)
    ->orderBy('name', 'ASC')
    ->limit(10)
    ->get();

// Insert and get ID
$id = $app->db->table('users')->insertGetId([
    'name'  => 'Alice',
    'email' => '[email protected]',
]);

// Update
$app->db->table('users')->where('id', '=', $id)->update(['active' => 0]);

// Paginate
$result = $app->db->table('posts')->orderBy('id', 'DESC')->paginate(15, 1);
// $result->data, $result->totalItems, $result->totalPages

class Post extends JiFramework\Core\Database\Model
{
    protected static string $table      = 'posts';
    protected static string $primaryKey = 'id';
}

Post::all();
Post::find(1);
Post::where('published', '=', 1)->orderBy('created_at', 'DESC')->get();
Post::create(['title' => 'Hello', 'body' => '...']);
Post::update(['title' => 'Updated'], 1);
Post::destroy(1);

$v = $app->validator->make($_POST, [
    'name'             => '    => '         => 'nullable|integer|min:18',
]);

if ($v->fails()) {
    $errors      = $v->errors();          // all errors
    $firstEmail  = $v->first('email');    // first error for a field
}

$v->throw(); // throws ValidationException on failure

$session = $app->sessionManager;

$session->set('user_id', 42);
$session->get('user_id');
$session->has('user_id');
$session->delete('user_id');

// Flash messages
$session->flashSuccess('Profile saved.');
$session->flashError('Something went wrong.');
$messages = $session->getFlashMessages();

// CSRF
$token = $session->generateCsrfToken();
$session->verifyCsrfToken($token); // true/false

$app->logger->info('User logged in', ['user_id' => 5]);
$app->logger->warning('Slow query detected', ['ms' => 450]);
$app->logger->error('Payment failed', ['order_id' => 99]);
// Also: debug(), notice(), critical(), alert(), emergency()

$enc = $app->encryption;

$key        = $enc->generateKey();          // 64-char hex key
$ciphertext = $enc->encrypt('secret', $key);
$plaintext  = $enc->decrypt($ciphertext, $key);

$hash = $enc->hashPassword('mypassword');
$enc->verifyPassword('mypassword', $hash);  // true

$token = $enc->randomBytes(32); // cryptographically secure token
bash
cp vendor/jahurul1/jiframework/jiconfig.example.php jiconfig.php