PHP code example of silviosln / arbor-router
1. Go to this page and download the library: Download silviosln/arbor-router 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/ */
silviosln / arbor-router example snippets
declare(strict_types=1);
ter = new Router([
'appDir' => __DIR__ . '/../app',
'security' => [
'headers' => true,
'csrf' => true,
],
]);
// Dispatches the complete lifecycle (security headers, matching, execution, and sending)
$router->dispatch();
// app/users/[id]/page.php
use Arbor\Router\Http\Request;
return function(Request $request, array $params): string {
$userId = htmlspecialchars((string) $params['id'], ENT_QUOTES, 'UTF-8');
return "<h1>User Profile #{$userId}</h1>";
};
// app/layoutroot.php (Global HTML Shell)
return function($request, $params): string {
return <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Arbor App</title>
</head>
<body class="bg-gray-50">
<main>{{content}}</main>
</body>
</html>
HTML;
};
// app/api/products/[slug]/route.php
use Arbor\Router\Http\Request;
use Arbor\Router\Http\JsonResponse;
return [
'GET' => function(Request $request, array $params) {
// ContentNegotiator automatically serializes to JSON or XML
return [
'slug' => $params['slug'],
'name' => 'Mechanical Keyboard',
'price' => 149.99,
];
},
'DELETE' => function(Request $request, array $params) {
return new JsonResponse(['deleted' => true], 200);
}
];
// app/products/create/action.php
use Arbor\Router\Http\Request;
use Arbor\Router\Action\ActionResult;
use Arbor\Router\Validation\Validator;
return function(Request $request, array $params, array $query, array $body, Validator $validator) {
// 1. Data Validation
$validation = $validator->make($body, [
'name' => 'created successfully!')
->data(['id' => $newId])
->redirect('/products/' . $newId);
};
// app/(dashboard)/middleware.php
use Arbor\Router\Http\RequestInterface;
use Arbor\Router\Http\RedirectResponse;
use Arbor\Router\Http\Response;
return function(RequestInterface $request, \Closure $next): Response {
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
if (!isset($_SESSION['user_id'])) {
return new RedirectResponse('/login');
}
return $next($request);
};
text
app/
├── layoutroot.php # Global HTML shell (<!DOCTYPE html>, <html>, <head>, <body>)
├── not-found.php # Custom 404 error page
├── error.php # Custom 500 error boundary
├── page.php # Homepage view ("/")
├── about/
│ └── page.php # Visual page ("/about")
├── (dashboard)/ # Route group: excluded from public URL
│ ├── layout.php # Shared dashboard layout (sidebar + header)
│ ├── middleware.php # Authentication guard for dashboard routes
│ └── admin/
│ ├── page.php # Admin page ("/admin")
│ └── products/
│ ├── [id]/
│ │ ├── page.php # View/Edit product ("/admin/products/123")
│ │ └── edit/
│ │ └── action.php # Form mutation action ("/admin/products/123/edit")
│ └── route.php # REST API endpoint ("/admin/products")
html
<form action="/products/create" method="POST">
<?= (new \Arbor\Router\Security\CsrfGuard())->field()