PHP code example of nihad1213 / litephp

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

    

nihad1213 / litephp example snippets


// Special case for login route (no authentication needed)
if ($path === "/login") {
    



 TaskController extends BaseController
{
    /**
     * Constructor
     */
    public function __construct(private TaskGateway $gateway) {}

    /**
     * Get all tasks
     */
    #[Route(path: 'tasks', method: 'GET')]
    public function getAll(): void
    {
        echo json_encode($this->gateway->getAll());
    }

    /**
     * Get a specific task by ID
     */
    #[Route(path: 'tasks/{id}', method: 'GET')]
    public function getOne(string $id): void
    {
        $resource = $this->gateway->get($id);

        if (!$resource) {
            $this->respondNotFound($id);
            return;
        }

        echo json_encode($resource);
    }

    // Additional methods for create, update, delete...
}



class TaskGateway
{
    private PDO $conn;

    public function __construct(Database $database)
    {
        $this->conn = $database->getConnect();
    }

    /**
     * Get all tasks
     * @return array
     */
    public function getAll(): array
    {
        $sql = "SELECT * FROM `tasks`";

        $stmt = $this->conn->query($sql);
        $data = [];

        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $data[] = $row;
        }

        return $data;
    }

    // Additional methods for get, create, update, delete...
}



class UserGateway
{
    private PDO $conn;

    public function __construct(Database $database)
    {
        $this->conn = $database->getConnect();
    }

    // Replace {tablename} with 'users' in all methods
    public function getAll(): array
    {
        $sql = "SELECT id, name, email, created_at FROM `users`";
        // Rest of the code...
    }
    
    // Customize other methods...
}



 UserController extends BaseController
{
    public function __construct(private UserGateway $gateway) {}

    // Replace {route} with 'users' in all Route attributes
    #[Route(path: 'users', method: 'GET')]
    public function getAll(): void
    {
        echo json_encode($this->gateway->getAll());
    }
    
    // Customize validation for your entity
    private function getValidationErrors(array $data, bool $is_new = true): array
    {
        $errors = [];

        if ($is_new && empty($data['name'])) {
            $errors[] = "Name is 
bash
php lite generate:key
bash
php lite start
bash
php lite start
bash
php lite key:generate
bash
php lite create:controller UserController
bash
php lite create:gateway UserGateway
bash
php lite db:migrate
bash
php lite db:migrate
bash
php lite create:gateway UserGateway
bash
php lite create:controller UserController