You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+222-4Lines changed: 222 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,9 @@ The goal is not to lock you into a fixed ecosystem. Each `maplephp/*` library is
22
22
-[Middleware](#middleware)
23
23
-[Database](#database)
24
24
-[Validation](#validation)
25
+
-[Aborting Requests](#aborting-requests)
25
26
-[Error Handling](#error-handling)
27
+
-[Twig Templates](#twig-templates)
26
28
-[Caching](#caching)
27
29
-[Logging](#logging)
28
30
-[CLI Commands](#cli-commands)
@@ -577,13 +579,13 @@ use MaplePHP\Validate\Validator;
577
579
578
580
// Option 1: Create an instance
579
581
$v = new Validator($email);
580
-
if ($v->isEmail() && $v->length(5, 255)) {
581
-
// value is valid
582
+
if (!($v->isEmail() && $v->length(5, 255))) {
583
+
abort(422, 'Invalid email address');
582
584
}
583
585
584
586
// Option 2: Use the static method for cleaner syntax
585
-
if (Validator::value($email)->isEmail(1, 200)) {
586
-
// value is valid
587
+
if (!Validator::value($email)->isEmail(1, 200)) {
588
+
abort(422, 'Invalid email address');
587
589
}
588
590
```
589
591
@@ -593,6 +595,222 @@ Visit the [maplephp/validate](https://github.com/maplephp/validate) repository f
593
595
594
596
---
595
597
598
+
## Aborting Requests
599
+
600
+
The global `abort()` function is the standard way to stop a request and return an HTTP error response. It throws an `HttpException` that the `HttpStatusError` middleware catches and renders as an error page.
601
+
602
+
```php
603
+
// Trigger a 404
604
+
abort(404);
605
+
606
+
// With a custom message
607
+
abort(404, 'User not found');
608
+
609
+
// With extra props forwarded to the error page renderer
In the error page renderer: `$context['redirect']` will be `'/login'`.
647
+
648
+
---
649
+
650
+
## Custom HTTP Error Pages
651
+
652
+
By default, `HttpStatusError` renders a built-in styled page for any `abort()` call or non-2xx response. The page shows the status code, a message, and a "Go home" link.
653
+
654
+
To replace it, implement `MaplePHP\Core\Interfaces\ErrorPageInterface`:
|`$context['message']`|`abort()` arg 2 | Custom message passed to `abort()`|
689
+
|`$context[*]`|`abort()` arg 3 | Any props from the third `abort()` argument |
690
+
|`$request->getUri()`| PSR-7 | The current request URI |
691
+
692
+
Register the custom renderer in `configs/http.php` by passing it directly to `HttpStatusError`:
693
+
694
+
```php
695
+
// configs/http.php
696
+
use App\Errors\MyErrorPage;
697
+
use MaplePHP\Core\Middlewares\HttpStatusError;
698
+
use MaplePHP\Emitron\Middlewares\ContentLengthMiddleware;
699
+
700
+
return [
701
+
"middleware" => [
702
+
"global" => [
703
+
new HttpStatusError(new MyErrorPage()),
704
+
ContentLengthMiddleware::class,
705
+
]
706
+
]
707
+
];
708
+
```
709
+
710
+
---
711
+
712
+
## Twig Templates
713
+
714
+
MaplePHP ships with built-in Twig support via `TwigServiceProvider` and the `MaplePHP\Core\Support\Twig` helper. Enable it by adding the provider to `configs/providers.php`:
715
+
716
+
```php
717
+
// configs/providers.php
718
+
return [
719
+
\MaplePHP\Core\Providers\DatabaseProvider::class,
720
+
\MaplePHP\Core\Providers\TwigServiceProvider::class, // Remove this line if you don't use Twig
721
+
];
722
+
```
723
+
724
+
The provider registers a `Twig\Environment` in the container, using `resources/` as the template root. It enables file caching in production and debug mode in development automatically.
725
+
726
+
### Template Structure
727
+
728
+
```
729
+
resources/
730
+
├── index.twig # Base layout — extend this in every view
731
+
├── views/ # View templates
732
+
│ └── hello.twig
733
+
└── errors/ # Error page templates (used by TwigErrorPage)
734
+
└── error.twig
735
+
```
736
+
737
+
### Layout and Inheritance
738
+
739
+
`resources/index.twig` is the base layout. Define your shared `<html>`, `<head>`, and chrome there, and expose named blocks for child templates to fill in:
740
+
741
+
A child view extends it and fills the blocks:
742
+
743
+
### Rendering in a Controller
744
+
745
+
Inject `MaplePHP\Core\Support\Twig` as a parameter — the framework resolves it automatically. Call `render()` with a path relative to `resources/` and an array of template variables:
746
+
747
+
```php
748
+
// app/Controllers/HelloController.php
749
+
namespace App\Controllers;
750
+
751
+
use MaplePHP\Core\Routing\DefaultController;
752
+
use MaplePHP\Core\Support\Twig;
753
+
use MaplePHP\Http\Interfaces\PathInterface;
754
+
use Psr\Http\Message\ResponseInterface;
755
+
756
+
class HelloController extends DefaultController
757
+
{
758
+
public function show(Twig $twig, PathInterface $path): void
`TwigErrorPage` renders `resources/errors/error.twig` and passes the following variables:
779
+
780
+
| Variable | Description |
781
+
|---|---|
782
+
|`{{ code }}`| HTTP status code (404, 500, …) |
783
+
|`{{ message }}`| Reason phrase or custom `abort()` message |
784
+
|`{{ uri }}`| The request URI that triggered the error |
785
+
|`{{ context }}`| Full context array from `abort()`|
786
+
787
+
---
788
+
789
+
## Error page
790
+
791
+
To render HTTP error responses with;
792
+
* Twig, register `TwigErrorPage` in `configs/http.php`:
793
+
* Vanilla, register `HttpStatusError::class` in `configs/http.php`:
794
+
795
+
```php
796
+
// configs/http.php
797
+
use MaplePHP\Core\Middlewares\HttpStatusError;
798
+
use MaplePHP\Core\Render\Errors\TwigErrorPage;
799
+
use MaplePHP\Emitron\Middlewares\ContentLengthMiddleware;
800
+
801
+
return [
802
+
"middleware" => [
803
+
"global" => [
804
+
// HttpStatusError::class, // Will load PHP vanilla HTTP Status Error page
805
+
// new HttpStatusError(new TwigErrorPage()), // Will load Twig HTTP Status Error page
806
+
ContentLengthMiddleware::class,
807
+
]
808
+
]
809
+
];
810
+
```
811
+
812
+
---
813
+
596
814
## Caching
597
815
598
816
`maplephp/cache` implements both PSR-6 (`CacheItemPoolInterface`) and PSR-16 (`CacheInterface`). The `Cache` wrapper provides the simple PSR-16 API around any PSR-6 handler.
0 commit comments