PHP code example of easeappphp / highper-blueprint

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

    

easeappphp / highper-blueprint example snippets




use EaseAppPHP\HighPer\Framework\Core\Application;

// Create the application
$app = new Application(__DIR__);

// Define a route
$app->getRouter()->get('/api/hello', function ($request) {
    return [
        'message' => 'Hello, World!',
        'timestamp' => time(),
    ];
});

// Run the application
$app->run();



namespace App\Controllers\Api;

use Amp\Http\Server\Request;
use Amp\Http\Server\Response;
use EaseAppPHP\HighPer\Framework\API\ApiController;

class UserController extends ApiController
{
    public function index(Request $request): Response
    {
        $users = $this->container->get(UserRepository::class)->all();
        return $this->success($users);
    }
    
    public function show(Request $request): Response
    {
        $id = $this->getParam($request, 'id');
        $user = $this->container->get(UserRepository::class)->find($id);
        
        if (!$user) {
            return $this->notFound('User not found');
        }
        
        return $this->success($user);
    }
}



namespace App\WebSocket;

use EaseAppPHP\HighPer\Framework\WebSocket\WebSocketConnection;
use EaseAppPHP\HighPer\Framework\WebSocket\WebSocketHandlerInterface;

class ChatHandler implements WebSocketHandlerInterface
{
    protected array $clients = [];
    
    public function onConnect(WebSocketConnection $connection): void
    {
        $this->clients[$connection->getId()] = $connection;
        $this->broadcast("User {$connection->getId()} joined the chat");
    }
    
    public function onMessage(WebSocketConnection $connection, $message): void
    {
        $this->broadcast("User {$connection->getId()}: {$message}");
    }
    
    public function onDisconnect(WebSocketConnection $connection): void
    {
        unset($this->clients[$connection->getId()]);
        $this->broadcast("User {$connection->getId()} left the chat");
    }
    
    protected function broadcast(string $message): void
    {
        foreach ($this->clients as $client) {
            if ($client->isOpen()) {
                $client->send($message);
            }
        }
    }
}
bash
php -S localhost:8080 public/index.php