PHP code example of omaressaouaf / plain-kit

1. Go to this page and download the library: Download omaressaouaf/plain-kit 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/ */

    

omaressaouaf / plain-kit example snippets




use Omaressaouaf\PlainKit\App;

ind(ClientService::class, fn () => new ClientService())
    ->run();



use Omaressaouaf\PlainKit\Router;

/** @var Router $router */

$router->get('/login', 'Auth/Login/create')->middleware('guest');
$router->post('/login', 'Auth/Login/store')->middleware('guest');
$router->get('/clients', 'Clients/index')->middleware('auth');
$router->post('/clients', 'Clients/store')->middleware('auth');
$router->get('/clients/{id}', 'Clients/show')->middleware('auth');



use Omaressaouaf\PlainKit\App;
use Omaressaouaf\PlainKit\Request;
use Omaressaouaf\PlainKit\Response;
use Omaressaouaf\PlainKit\Session;
use Http\Forms\StoreClientForm;
use Services\ClientService;

$request = App::resolve(Request::class);
$response = App::resolve(Response::class);
$session = App::resolve(Session::class);
$clientService = App::resolve(ClientService::class);

StoreClientForm::validate();

$clientService->create($request->input('name'));

$session->flash('success', 'Client created successfully!');

$response->back();



return [
    'database' => [
        'connection' => [
            'host' => env('DB_HOST', 'localhost'),
            'port' => (int) env('DB_PORT', 3306),
            'dbname' => env('DB_DATABASE', 'plain_kit_ledger'),
            'charset' => 'utf8mb4',
        ],
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', ''),
    ],
];

load_env('/path/to/app');   // usually called automatically by App::create()
env('DB_HOST', 'localhost'); // read a variable with optional default

App::create(dirname(__DIR__))
    ->bind(UserRepository::class, fn () => new UserRepository())
    ->bind(RegisterService::class, fn () => new RegisterService())
    ->run();

->bind(ClientService::class, fn () => new ClientService())

$request = App::resolve(Request::class);
$userRepository = App::resolve(UserRepository::class);

$router->get('/reports', 'Reports/index');
$router->post('/transactions', 'Transactions/store');
$router->delete('/logout', 'Auth/Login/destroy');
$router->put('/items/{id}', 'Items/update');
$router->patch('/items/{id}', 'Items/patch');

$router->get('/clients', 'Clients/index')->middleware('auth');
$router->get('/login', 'Auth/Login/create')->middleware('guest');

$router->get('/clients/{id}', 'Clients/show');

$id = $request->params('id');
$all = $request->params(); // all route params as array

$router->get('/clients/list', 'Clients/list');   // match first
$router->get('/clients/{id}', 'Clients/show');     // match second

$request = App::resolve(Request::class);

$request->uri();                          // "/clients"
$request->abs_uri();                      // "/clients?page=2"
$request->method();                         // "GET", "POST", etc.
$request->input('email');                 // from $_GET or $_POST
$request->input('email', 'default');      // with fallback
$request->params('id');                   // route parameter
$request->params();                       // all route parameters

$response = App::resolve(Response::class);

// Render a view from app/Views/{name}.view.php
$response->view('clients', ['clients' => $clients]);

// JSON
$response->json(['reports' => $reports]);

// Redirect
$response->redirect('/login');

// Go back to the previous page (falls back to "/")
$response->back();

// Abort with an HTTP status code
$response->abort(404);
$response->abort(419); // CSRF failure

$response->view('Partials/head');
$response->view('Partials/nav');

<p>Welcome, <?= e($user['name']) 

$session = App::resolve(Session::class);

$session->put('user', $user);
$session->get('user');
$session->has('user');
$session->forget('user');

// Flash data (available on the next request)
$session->flash('success', 'Saved!');
$session->flash('errors', ['email' => 'Invalid email']);
$session->get('success');

$csrf = App::resolve(Csrf::class);



namespace Http\Forms;

use Omaressaouaf\PlainKit\Form;
use Omaressaouaf\PlainKit\Validator;

class LoginForm extends Form
{
    protected function handle(): void
    {
        if (! Validator::exists($this->request->input('email'))
            || ! Validator::email($this->request->input('email'))) {
            $this->addError('email', 'Email must be valid');
        }

        if (! Validator::exists($this->request->input('password'))) {
            $this->addError('password', 'Password is 

LoginForm::validate();

$errors = $session->get('errors');

$database = App::resolve(Database::class);

$users = $database
    ->query('SELECT * FROM users WHERE email = :email', ['email' => $email])
    ->get();

$user = $database
    ->query('SELECT * FROM users WHERE id = :id', ['id' => $id])
    ->find();

$user = $database
    ->query('SELECT * FROM users WHERE id = :id', ['id' => $id])
    ->findOrFail(); // aborts with 404 if not found



namespace Repositories;

use Omaressaouaf\PlainKit\App;
use Omaressaouaf\PlainKit\Database;

class ClientRepository
{
    private Database $database;

    public function __construct()
    {
        $this->database = App::resolve(Database::class);
    }

    public function get(): array
    {
        return $this->database
            ->query('SELECT * FROM clients ORDER BY id DESC')
            ->get();
    }
}

$authenticator = App::resolve(Authenticator::class);

// Login attempt
if ($authenticator->attempt($email, $password)) {
    $response->redirect('/');
}

// Current user
$authenticator->user();   // array|null
$authenticator->check();   // bool

// Logout
$authenticator->logout();

// Update password for the logged-in user
$authenticator->updatePassword($newPassword);

interface MiddlewareInterface
{
    public function handle(): void;
}
txt
my-app/
├── app/
│   ├── Http/
│   │   ├── Controllers/
│   │   └── Forms/
│   ├── Repositories/
│   ├── Services/
│   └── Views/
├── config/
│   └── app.php
├── database/
│   └── migrate.php
├── public/
│   └── index.php
├── routes.php
├── .env
└── .env.example
txt
app/Http/Controllers/Failures/404.php
sh
composer install
cp examples/ledger/.env.example examples/ledger/.env
php examples/ledger/database/migrate.php
php -S localhost:8080 -t examples/ledger/public