PHP code example of rad / rad-framework

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

    

rad / rad-framework example snippets





 new \Rad\Rad(__DIR__ . "/config/");

...




use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rad\Route\Attribute\Get;
use Rad\Route\Attribute\Produce;

class Example extends \Rad\Controller\Controller {

    #[Get('/'), Produce('html')]
    public function html(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface {
        $response->getBody()->write("<b>Hello World</b>");
        return $response;
    }

}


$app->addControllers([
    Example::class
]);


$app->run();


$app->run(function(){
	echo "End";
});

use Rad\Container\Container;

class UserController extends \Rad\Controller\Controller {

    public function __construct(\Rad\Route\Route $route = null, private ?UserRepository $users = null) {
        parent::__construct($route);
    }
}

$app = new \Rad\Rad(__DIR__ . '/config/');

$container = $app->getContainer();
$container->bind(UserRepositoryInterface::class, MysqlUserRepository::class);
$container->singleton(Clock::class, fn() => new SystemClock());
$container->instance(SomeService::class, $alreadyBuilt);

class ReportController extends \Rad\Controller\Controller {

    public function __construct(
        \Rad\Route\Route $route = null,
        private ?\Psr\Log\LoggerInterface $log = null,          // Log::getHandler()
        private ?\Rad\Cache\CacheInterface $cache = null,       // Cache::getHandler()
        private ?\Rad\Database\DatabaseAdapter $db = null       // Database::getHandler()
    ) {
        parent::__construct($route);
    }
}

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

final class Timing implements MiddlewareInterface {
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
        $start = microtime(true);
        return $handler->handle($request)
            ->withHeader('X-Elapsed-Ms', (string) round((microtime(true) - $start) * 1000, 2));
    }
}

use Rad\Route\Attribute\Get;
use Rad\Route\Attribute\Middleware;

#[Get('/'), Middleware(Timing::class)]
public function index(...): ResponseInterface { /* ... */ }

use Rad\Event\AbstractEvent;
use Rad\Event\EventListenerInterface;

final class UserRegistered extends AbstractEvent {
    public function __construct(public readonly int $userId) {}
}

final class SendWelcomeEmail implements EventListenerInterface {
    public function handle(object $event): void {
        if ($event instanceof UserRegistered) { /* ... */ }
    }
}

$dispatcher = \Rad\Event\Event::getHandler();          // PSR-14 EventDispatcherInterface
if ($dispatcher instanceof \Rad\Event\EventHandler) {
    $dispatcher->addListener(UserRegistered::class, new SendWelcomeEmail());
}

// From anywhere (a controller can also use $this->dispatch($event)):
$dispatcher->dispatch(new UserRegistered(42));

use Rad\Http\HttpFactory;

$factory = new HttpFactory();
$client  = $app->getContainer()->get(\Psr\Http\Client\ClientInterface::class);

$request  = $factory->createRequest('GET', 'https://api.example.com/status');
$response = $client->sendRequest($request);   // throws Rad\Http\Exception\NetworkException on transport failure

echo $response->getStatusCode();
echo (string) $response->getBody();
bash

php -v