PHP code example of jefyokta / oktaax

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

    

jefyokta / oktaax example snippets




ktaax\Oktaax;
use Oktaax\Http\Request;
use Oktaax\Http\Response;

$app = new Oktaax;

$app->get("/", function (Request $request, Response $response) {
    $response->end("Hello World");
});

$app->listen(3000);



ktaax\Oktaax;
use Oktaax\Websocket\HasWebsocket;
use Oktaax\Http\Request;
use Oktaax\Http\Response;
use Oktaax\Websocket\Client;
use Oktaax\Websocket\Server;

$app = new class extends Oktaax {
    use HasWebsocket;
};

$app->get("/user", function (Request $request, Response $response) {
    $response->end("User endpoint hit!");
});

$app->ws('welcome', function (Server $server, Client $client) {
    $server->reply($client, "Hi Client {$client->fd}");
});

$app->listen(3000);



use Oktaax\Interfaces\Channel;
use Oktaax\Websocket\Client;

class EvenChannel implements Channel
{
    public function eligable(Client $client): bool
    {
        return $client->fd % 2 === 0;
    }
}



$app->ws('even', function (Server $server, Client $client) {
    $server->toChannel(EvenChannel::class)->broadcast(function ($client) {
        return "Hello {$client->fd}! You have an even fd!";
    });
});



$app = new Oktaax;

$app
    ->get("/", fn($request, $response) => $response->end("Welcome to Oktaax"))
    ->get("/user/{username}", function ($request, $response) {
        $user = $request->params['username'];
        $response->end("Hello, {$user}");
    })
    ->post("/user", function ($request, $response) {
        $data = $request->post;
        $response->end("User created with data: " . json_encode($data));
    })
    ->put("/user/{id}", function ($request, $response) {
        $id = $request->params['id'];
        $response->end("User {$id} updated");
    })
    ->delete("/user/{id}", function ($request, $response) {
        $id = $request->params['id'];
        $response->end("User {$id} deleted");
    });

$app->listen(3000);
bash
php index.php
bash
php index.php