update docs for namespace
This commit is contained in:
parent
8a94183081
commit
405dcae482
34
EXAMPLES.md
34
EXAMPLES.md
@ -6,9 +6,12 @@ This file contains practical examples demonstrating how to use the Web framework
|
|||||||
|
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
require_once 'Web.php';
|
|
||||||
|
|
||||||
$app = new Web(debug: true);
|
namespace Web;
|
||||||
|
|
||||||
|
require_once 'App.php';
|
||||||
|
|
||||||
|
$app = new App(debug: true);
|
||||||
|
|
||||||
$app->get('/', function(Context $context) {
|
$app->get('/', function(Context $context) {
|
||||||
return 'Hello World!';
|
return 'Hello World!';
|
||||||
@ -22,13 +25,13 @@ $app->run();
|
|||||||
?>
|
?>
|
||||||
```
|
```
|
||||||
|
|
||||||
## Web Application Examples
|
## App (Application) Examples
|
||||||
|
|
||||||
### Basic Setup
|
### Basic Setup
|
||||||
|
|
||||||
```php
|
```php
|
||||||
// Create application with debug mode
|
// Create application with debug mode
|
||||||
$app = new Web(debug: true);
|
$app = new App(debug: true);
|
||||||
|
|
||||||
// Add global middleware
|
// Add global middleware
|
||||||
$app->use(function(Context $context, callable $next) {
|
$app->use(function(Context $context, callable $next) {
|
||||||
@ -46,7 +49,7 @@ $app->delete('/path', $handler);
|
|||||||
$app->head('/path', $handler);
|
$app->head('/path', $handler);
|
||||||
|
|
||||||
// Route groups
|
// Route groups
|
||||||
$app->group('/api', function(Web $app) {
|
$app->group('/api', function(App $app) {
|
||||||
$app->get('/users', $usersHandler);
|
$app->get('/users', $usersHandler);
|
||||||
$app->post('/users', $createUserHandler);
|
$app->post('/users', $createUserHandler);
|
||||||
});
|
});
|
||||||
@ -56,7 +59,7 @@ $app->setErrorHandler(404, function(Context $context) {
|
|||||||
$context->json(['error' => 'Custom 404'], 404);
|
$context->json(['error' => 'Custom 404'], 404);
|
||||||
});
|
});
|
||||||
|
|
||||||
$app->setDefaultErrorHandler(function(Context $context, int $status, string $message, ?Exception $e) {
|
$app->setDefaultErrorHandler(function(Context $context, int $status, string $message, ?\Exception $e) {
|
||||||
$context->json(['error' => $message, 'status' => $status], $status);
|
$context->json(['error' => $message, 'status' => $status], $status);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -363,7 +366,7 @@ $allCookies = $context->cookie->all();
|
|||||||
$context->cookie->clear();
|
$context->cookie->clear();
|
||||||
|
|
||||||
// Configure cookie defaults for the application
|
// Configure cookie defaults for the application
|
||||||
$app = new Web(debug: true, cookieDefaults: [
|
$app = new App(debug: true, cookieDefaults: [
|
||||||
'path' => '/',
|
'path' => '/',
|
||||||
'domain' => '.example.com',
|
'domain' => '.example.com',
|
||||||
'secure' => true,
|
'secure' => true,
|
||||||
@ -566,7 +569,7 @@ $errorHandler->register(404, function(Context $context, int $status, string $mes
|
|||||||
$context->json(['error' => 'Custom not found'], 404);
|
$context->json(['error' => 'Custom not found'], 404);
|
||||||
});
|
});
|
||||||
|
|
||||||
$errorHandler->setDefaultHandler(function(Context $context, int $status, string $message, ?Exception $e) {
|
$errorHandler->setDefaultHandler(function(Context $context, int $status, string $message, ?\Exception $e) {
|
||||||
$context->json(['error' => $message, 'code' => $status], $status);
|
$context->json(['error' => $message, 'code' => $status], $status);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -695,11 +698,14 @@ $app->post('/upload', function(Context $context) {
|
|||||||
|
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
require_once 'Web.php';
|
|
||||||
|
namespace Web;
|
||||||
|
|
||||||
|
require_once 'App.php';
|
||||||
require_once 'auth/Auth.php';
|
require_once 'auth/Auth.php';
|
||||||
require_once 'auth/AuthMiddleware.php';
|
require_once 'auth/AuthMiddleware.php';
|
||||||
|
|
||||||
$app = new Web(debug: true);
|
$app = new App(debug: true);
|
||||||
|
|
||||||
// Setup auth
|
// Setup auth
|
||||||
$auth = new Auth($app->context->session);
|
$auth = new Auth($app->context->session);
|
||||||
@ -737,7 +743,7 @@ $app->post('/logout', function(Context $context) use ($auth) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Protected routes
|
// Protected routes
|
||||||
$app->group('/api', function(Web $app) use ($authMiddleware) {
|
$app->group('/api', function(App $app) use ($authMiddleware) {
|
||||||
$app->use($authMiddleware->requireAuth());
|
$app->use($authMiddleware->requireAuth());
|
||||||
$app->use($authMiddleware->rateLimit(100, 1));
|
$app->use($authMiddleware->rateLimit(100, 1));
|
||||||
|
|
||||||
@ -791,7 +797,7 @@ $app->group('/api', function(Web $app) use ($authMiddleware) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Admin routes
|
// Admin routes
|
||||||
$app->group('/admin', function(Web $app) use ($authMiddleware) {
|
$app->group('/admin', function(App $app) use ($authMiddleware) {
|
||||||
$app->use($authMiddleware->requireRole('admin'));
|
$app->use($authMiddleware->requireRole('admin'));
|
||||||
|
|
||||||
$app->get('/users', function(Context $context) {
|
$app->get('/users', function(Context $context) {
|
||||||
@ -813,7 +819,7 @@ $app->group('/admin', function(Web $app) use ($authMiddleware) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Public API routes
|
// Public API routes
|
||||||
$app->group('/public', function(Web $app) {
|
$app->group('/public', function(App $app) {
|
||||||
$app->get('/posts', function(Context $context) {
|
$app->get('/posts', function(Context $context) {
|
||||||
$posts = getPublicPosts();
|
$posts = getPublicPosts();
|
||||||
return ['posts' => $posts];
|
return ['posts' => $posts];
|
||||||
@ -861,7 +867,7 @@ function testRoute($method, $path, $data = []) {
|
|||||||
$_POST = $data;
|
$_POST = $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
$app = new Web();
|
$app = new App();
|
||||||
// Setup your routes...
|
// Setup your routes...
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
|
|||||||
13
README.md
13
README.md
@ -6,9 +6,12 @@ Web is a lightweight, high-performance PHP web framework built around a hyper-op
|
|||||||
|
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
require_once 'Web.php';
|
|
||||||
|
|
||||||
$app = new Web(debug: true);
|
namespace Web;
|
||||||
|
|
||||||
|
require_once 'App.php';
|
||||||
|
|
||||||
|
$app = new App(debug: true);
|
||||||
|
|
||||||
$app->get('/', function(Context $context) {
|
$app->get('/', function(Context $context) {
|
||||||
return 'Hello World!';
|
return 'Hello World!';
|
||||||
@ -26,7 +29,7 @@ See [EXAMPLES.md](EXAMPLES.md) for comprehensive examples and usage patterns.
|
|||||||
|
|
||||||
## Core Components
|
## Core Components
|
||||||
|
|
||||||
### Web Application
|
### App (Application)
|
||||||
The main application class that handles routing, middleware, and request processing.
|
The main application class that handles routing, middleware, and request processing.
|
||||||
- Route registration (`get()`, `post()`, `put()`, `patch()`, `delete()`, `head()`)
|
- Route registration (`get()`, `post()`, `put()`, `patch()`, `delete()`, `head()`)
|
||||||
- Middleware support (`use()`)
|
- Middleware support (`use()`)
|
||||||
@ -107,9 +110,9 @@ Comprehensive error and exception handling.
|
|||||||
|
|
||||||
## API Reference
|
## API Reference
|
||||||
|
|
||||||
### Web Class
|
### App Class
|
||||||
```php
|
```php
|
||||||
new Web(bool $debug = false, array $cookieDefaults = [])
|
new App(bool $debug = false, array $cookieDefaults = [])
|
||||||
use(callable $middleware): self
|
use(callable $middleware): self
|
||||||
get|post|put|patch|delete|head(string $route, callable $handler): self
|
get|post|put|patch|delete|head(string $route, callable $handler): self
|
||||||
group(string $prefix, callable $callback): self
|
group(string $prefix, callable $callback): self
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user