aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 97be075b3a969e39207240d4625d4fc0ba70dc76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# miniroute

A small attribute-based routing and middleware kernel for PHP applications.

miniroute is the tiny piece of routing machinery extracted from
[brett-parson.com](https://brett-parson.com). It provides attribute-declared
routes, deterministic route matching, and an onion-style middleware pipeline —
without being a framework.

## Requirements

- PHP >= 8.2

## Install

### Via Composer (VCS repository)

```bash
composer config repositories.miniroute vcs https://src.brett-parson.com/git/miniroute.git
composer require brettparson/miniroute:^0.1
```

### Via a local path repository (development)

```json
{
    "repositories": [
        { "type": "path", "url": "../miniroute", "options": { "symlink": true } }
    ],
    "require": { "brettparson/miniroute": "@dev" }
}
```

## Usage

```php
use BrettParson\MiniRoute\Attribute\Get;
use BrettParson\MiniRoute\Attribute\Middleware;
use BrettParson\MiniRoute\Attribute\Post;
use BrettParson\MiniRoute\Http\RequestInterface;
use BrettParson\MiniRoute\Http\ResponseInterface;
use BrettParson\MiniRoute\Routing\Router;

final class NoteController
{
    #[Get('/notes')]
    public function index(RequestInterface $request): ResponseInterface
    {
        // ...
    }

    #[Post('/notes')]
    #[Middleware(SomeMiddleware::class)]
    public function store(RequestInterface $request): ResponseInterface
    {
        // ...
    }
}
```

The router needs a controller/middleware resolver — an app-owned seam for
building objects:

```php
$router = new Router($resolver);

$router->group('/admin', [RequireAdmin::class, RequireCsrf::class]);

$router->registerControllers([
    HomeController::class,
    NoteController::class,
]);

$response = $router->dispatch($request);
$response->send();
```

## What it does

- **Route attributes**`#[Get]`, `#[Post]`, `#[Put]`, `#[Patch]`, `#[Delete]`
  declare routes directly on controller methods.
- **Deterministic matching** — literal segments beat `{parameter}` segments,
  so registration order never matters.
- **Middleware**`#[Middleware(...)]` on methods or classes, plus
  router-level `group()` middleware by path prefix.
- **Thin HTTP contracts**`RequestInterface` and `ResponseInterface` keep
  the kernel decoupled from any one application's HTTP objects.

## What it is not

No container, no template engine, no ORM, no auth, no session handling, no
configuration loader. Those stay application concerns.

## Tests

```bash
composer install
vendor/bin/phpunit
```

Or, without Composer, drop a `phpunit.phar` into the repo root and run
`php phpunit.phar`.

## License

MIT — see [LICENSE](LICENSE).