1. Go to this page and download the library: Download nimbly/limber 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/ */
nimbly / limber example snippets
eate a Router instance and define a route.
$router = new Nimbly\Limber\Router\Router;
$router->get("/", fn() => new Nimbly\Capsule\Response(200, "Hello World!"));
// Create Application instance with router.
$application = new Nimbly\Limber\Application($router);
// Dispatch a PSR-7 ServerRequestInterface instance and get back a PSR-7 ResponseInterface instance
$response = $application->dispatch(
Nimbly\Capsule\Factory\ServerRequestFactory::createFromGlobals()
);
// Send the ResponseInterface instance
$application->send($response);
// Create PSR-11 container instance and configure as needed.
$container = new Container;
$container->set(
Foo:class,
fn(): Foo => new Foo(\getenv("FOO_NAME"))
);
// Create Application instance with router and container.
$application = new Nimbly\Limber\Application(
router: $router,
container: $container
);
class SampleMiddleware implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
// Add a custom header to the request before sending to route handler
$request = $request->withAddedHeader("X-Foo", "Bar");
$response = $handler->handle($request);
// Add a custom header to the response before sending back to client
return $response->withAddedHeader("X-Custom-Header", "Foo");
}
}
class BooksHandler
{
public function getByIsbn(ServerRequestInterface $request, string $isbn): ResponseInterface
{
$book = BookModel::findByIsbn($isbn);
if( empty($book) ){
throw new NotFoundHttpException("ISBN not found.");
}
return new JsonResponse(
200,
$book->toArray()
);
}
}
// Get a book by its ID and match the ID to a UUID.
$router->get("/books/{id:uuid}", "BooksHandler@get");
Router::setPattern("isbn", "\d{9}[\d|X]");
$router = new Router;
$router->get("/books/{id:isbn}", "BooksHandler@getByIsbn");
// Closure based handler
$router->get(
"/books/{id:isbn}",
function(ServerRequestInterface $request, string $id): ResponseInterface {
$book = Books::find($id);
if( empty($book) ){
throw new NotFoundHttpException("Book not found.");
}
return new Response(200, \json_encode($book));
}
);
// String references to ClassName@Method
$router->patch("/books/{id:isbn}", "App\Handlers\BooksHandler@update");
// If a ContainerInterface instance was assigned to the application and contains an InventoryService instance, it will be injected into this handler.
$router->post(
"/books",
function(ServerRequestInterface $request, InventoryService $inventoryService): ResponseInterface {
$book = Book::make($request->getParsedBody());
$inventoryService->add($book);
return new Response(201, \json_encode($book));
}
);
$router->group(
hostnames: ["sub.domain.com"],
middleware: [
FooMiddleware::class,
BarMiddleware::class
],
namespace: "\App\Sub.Domain\Handlers",
prefix: "v1",
routes: function(Router $router): void {
$router->get("books/{isbn}", "BooksHandler@getByIsbn");
$router->post("books", "BooksHandler@create");
// This group will inherit all group settings from the parent group, override
// the namespace property, and will merge in an additional middleware (AdminMiddleware).
$router->group(
namespace: "\App\Sub.Domain\Handlers\Admin",
middleware: [
AdminMiddleware::class
],
routes: function(Router $router): void {
$route->delete("books/{isbn}", "BooksHandler@deleteBook");
}
);
}
);
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Nimbly\Capsule\Response;
// Create the router and some routes.
$router = new Nimbly\Limber\Router;
$router->get("/", function(ServerRequestInterface $request): ResponseInterface {
return new Response(
"Hello world!"
);
});
// Create the Limber Application instance.
$application = new Nimbly\Limber\Application($router);
// Create the HTTP server to handle incoming HTTP requests with your Limber Application instance.
$httpServer = new React\Http\HttpServer(
function(ServerRequestInterface $request) use ($application): ResponseInterface {
return $application->dispatch($request);
}
);
// Listen on port 8000.
$httpServer->listen(
new React\Socket\SocketServer("0.0.0.0:8000");
);
$loop = React\EventLoop\Loop::get();
$loop->addSignal(
SIGINT,
function(int $signal) use ($loop): void {
\error_log("SIGINT received: Shutting down gracefully.");
$loop->stop();
}
);
docker
FROM php:8.2-cli
RUN apt-get update && apt-get upgrade --yes
RUN curl --silent --show-error https://getcomposer.org/installer | php && \
mv composer.phar /usr/bin/composer
RUN mkdir -p /usr/src/php/ext && curl --silent https://pecl.php.net/get/ev-1.1.5.tgz | tar xvzf - -C /usr/src/php/ext
# Add other PHP modules
RUN docker-php-ext-install pcntl ev-1.1.5
WORKDIR /opt/service
ADD . .
RUN composer install --no-dev
CMD [ "php", "main.php" ]
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.