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\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();
$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
$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;
}