PHP code example of nexus-actors / http

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

    

nexus-actors / http example snippets


use Monadial\Nexus\Core\Actor\ActorSystem;
use Monadial\Nexus\Http\Dsl\HttpApp;
use Monadial\Nexus\Http\Response\JsonResponse;
use Monadial\Nexus\Runtime\Fiber\FiberRuntime;

$system = ActorSystem::create('my-app', new FiberRuntime());

$app = HttpApp::create($system)
    ->get('/hello', static fn() => JsonResponse::ok(['msg' => 'hello']));

// Hand the compiled app to a server adapter (e.g. nexus-http-server-swoole).
// compile() returns a CompiledHttpApp — an immutable, PSR-15 RequestHandlerInterface.
$server->serve($app->compile());

use Monadial\Nexus\Core\Actor\ActorRef;
use Monadial\Nexus\Http\Handler\Attribute\FromActor;
use Psr\Http\Message\ServerRequestInterface;

$app->actor('user-store', $userStoreProps)->workerLocal();
$app->perRequestActor('order-saga', $sagaProps);

$app->get('/users/{id}', static fn(
    ServerRequestInterface $r,
    #[FromActor('user-store')] ActorRef $store,
) => /* use $store->ask(...)->await() */);

use Monadial\Nexus\Http\Handler\Attribute\FromService;

$app->get('/orders', static fn(
    #[FromService('order.repository')] OrderRepository $repo,
) => JsonResponse::ok($repo->listAll()));

// Or resolve by parameter type (no id needed):
$app->get('/users', static fn(
    #[FromService] UserService $users,
) => JsonResponse::ok($users->all()));

use Monadial\Nexus\Core\Concurrent\Future;

$app->get('/dashboard', static function (
    #[FromActor('user-store')] ActorRef $users,
    #[FromActor('orders')] ActorRef $orders,
): Future {
    return Future::all([
        'user'   => $users->ask(new GetUser(1), Duration::seconds(1)),
        'orders' => $orders->ask(new ListOrders(1), Duration::seconds(1)),
    ])->map(static fn(FutureResult $r) => JsonResponse::ok($r->toArray()));
});