1. Go to this page and download the library: Download strictlyphp/dolphpin 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/ */
strictlyphp / dolphpin example snippets
declare(strict_types=1);
namespace App\Controllers;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use StrictlyPHP\Dolphin\Attributes\Route;
use StrictlyPHP\Dolphin\Request\Method;
use StrictlyPHP\Dolphin\Response\JsonResponse;
#[Route(Method::POST, '/users')]
class CreateUserController
{
public function __invoke(CreateUserDto $dto, ServerRequestInterface $request): ResponseInterface
{
// $dto is automatically mapped from the JSON body
return new JsonResponse(['id' => '123', 'name' => $dto->name], 201);
}
}
declare(strict_types=1);
namespace App\Controllers;
readonly class CreateUserDto
{
public function __construct(
public string $name,
public EmailAddress $email,
) {
}
}
use StrictlyPHP\Dolphin\App;
function main(array $event, object $context): array
{
$app = App::build(
controllers: ['App\Controllers'],
);
return $app->run($event, $context);
}
#[Route(Method::GET, '/users/{id}')]
class GetUserController { /* ... */ }
#[Route(Method::DELETE, '/users/{id}')]
class DeleteUserController { /* ... */ }
#[Route(Method::POST, '/admin/settings')]
#[RequiresRoles(['ADMIN'])]
class UpdateSettingsController { /* ... */ }
use StrictlyPHP\Dolphin\Authentication\AuthenticatedUserInterface;
class AuthMiddleware implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$user = // ... resolve authenticated user
$request = $request->withAttribute('user', $user);
return $handler->handle($request);
}
}
#[RequiresRoles([UserRole::ADMIN, 'SUPPORT'])]
class UpdateSettingsController { /* ... */ }
#[RequiresRole(UserRole::ADMIN)]
#[RequiresRole(UserRole::SUPPORT)] // ADMIN or SUPPORT
class UpdateSettingsController { /* ... */ }
use StrictlyPHP\Dolphin\Authorization\PermissionInterface;
use StrictlyPHP\Dolphin\Authorization\RoleInterface;
enum UserKind: string implements RoleInterface
{
case USER = 'USER';
case ADMIN = 'ADMIN';
}
enum AdminPermission: string implements PermissionInterface
{
case CREATE_REPORT = 'CREATE_REPORT';
case DELETE_REPORT = 'DELETE_REPORT';
}
use StrictlyPHP\Dolphin\Authorization\AuthorizationServiceInterface;
$app = App::build(
controllers: ['App\Controllers'],
containerDefinitions: [
AuthorizationServiceInterface::class => fn() => new MyAuthorizationService(),
],
middlewares: [AuthMiddleware::class], // sets the 'user' request attribute
);