1
1
# mindplay/timber
2
2
3
- [ ![ PHP Version] ( https://img.shields.io/badge/php-7.3 %2B-blue.svg )] ( https://packagist.org/packages/mindplay/timber )
4
- [ ![ Build Status ] ( https://travis-ci.org /mindplay-dk/timber.svg )] ( https://travis- ci.org/mindplay-dk/timber )
3
+ [ ![ PHP Version] ( https://img.shields.io/badge/php-8.0 %2B-blue.svg )] ( https://packagist.org/packages/mindplay/timber )
4
+ [ ![ Build status ] ( https://github.com /mindplay-dk/timber/actions/workflows/ ci.yml/badge.svg )
5
5
6
- Timber is a request router with regular expression support, high performance, and a
6
+ Timber is a router library with regular expression support, high performance, and a
7
7
developer-friendly, human-readable API.
8
8
9
- Originally a fork of [ TreeRoute] ( https://github.com/baryshev/TreeRoute ) by
10
- [ Vadim Baryshev] ( https://github.com/baryshev ) , the API and feature-set quickly
11
- grew into something else entirely. What does carry over from the original fork,
12
- is great performance.
13
-
14
9
15
10
## Installation
16
11
@@ -19,28 +14,22 @@ Install the latest version with `composer require mindplay/timber`
19
14
20
15
## Introduction
21
16
22
- The core router system is independent of some other optional features - this includes
23
- a Dispatcher and a UrlHelper, both of which work great together with the router, but
24
- the router itself is not dependent on either of these components.
17
+ This package provides a minimal router facility: a place to register path patterns, and
18
+ a means of resolving these to controller names.
25
19
26
20
27
21
## Usage
28
22
29
23
In the examples below, we assume that handlers (attached using ` get() ` , ` post() ` , etc.)
30
- are controller class-names - the included Dispatcher (see below) uses a class-per-action
31
- strategy, which may not be everyone's cup of tea, but the point is that, the meaning or
32
- interpretation of the handler string isn't baked into the router; it's supported by the
33
- Dispatcher only, and as such, you can easily implement any convention or strategy you
34
- like, either by doing your own dispatch (without using our Dispatcher) or by extending
35
- the Dispatcher and overriding the ` toCallable() ` function.
24
+ are controller class-names - in your application, they might be component IDs for a DI
25
+ container, file-names, or whatever else you like.
36
26
37
- Note the use of the magic ` ::class ` constant in the following examples - the library is
38
- compatible with PHP 5.4, but this syntax is only supported by PHP 5.5 and up.
39
-
40
- Basic usage of the router, without Dispatcher, goes a little something like this:
27
+ Basic usage of the router looks like this:
41
28
42
29
``` PHP
43
30
use mindplay\timber\Router;
31
+ use mindplay\timber\Result;
32
+ use mindplay\timber\Error;
44
33
45
34
require __DIR__ . '/vendor/autoload.php';
46
35
@@ -71,28 +60,17 @@ $url = '/news/1';
71
60
72
61
$result = $router->resolve($method, $url);
73
62
74
- if (! $result->error ) {
75
- $handler = $ result->handler ;
76
- $params = $result->params;
77
- // Do something with handler and params
63
+ if ($result instanceof Error ) {
64
+ header("HTTP/1.1 {$result->status} ${ result->message}") ;
65
+ // ...error response here...
66
+ return;
78
67
} else {
79
- switch ($result->error->code) {
80
- case 404 :
81
- // Not found handler here
82
- break;
83
- case 405 :
84
- // Method not allowed handler here
85
- $allowedMethods = $result->error->allowed;
86
- if ($method == 'OPTIONS') {
87
- // OPTIONS method handler here
88
- }
89
- break;
90
- }
68
+ // ...dispatch $result->handler with $result->params...
91
69
}
92
70
```
93
71
94
- If you're building a set of routes under the same parent route, you can continue routing
95
- from by keeping a temporary reference to a parent route - for example:
72
+ If you're building a set of routes under the same parent route, you can continue building
73
+ from a parent route - for example:
96
74
97
75
``` PHP
98
76
$admin = $router->route('admin')->get(AdminMenu::class);
@@ -103,8 +81,7 @@ $admin->route('groups')->get(AdminGroupList::class);
103
81
104
82
This example will route ` /admin ` to ` AdminMenu ` , and ` /admin/users ` to ` AdminUserList ` , etc.
105
83
106
- Continuing from a parent route avoids the overhead of repeatedly parsing the same parent path,
107
- and also enables modular reuse of route definitions - for example:
84
+ This also feature enables modular reuse of route definitions - for example:
108
85
109
86
``` PHP
110
87
$build_comment_routes = function (Route $parent) {
@@ -120,43 +97,6 @@ This example creates two identical sets of routes for displaying and posting com
120
97
different parent routes.
121
98
122
99
123
- ### Dispatch
124
-
125
- We also provide a [ Dispatcher] ( src/Dispatcher.php ) class and [ Controller] ( src/Controller.php )
126
- interface, following a simple convention of handler names being class-names, and a
127
- class-per-action strategy:
128
-
129
- ``` PHP
130
- use mindplay\timber\Router;
131
- use mindplay\timber\Controller;
132
- use mindplay\timber\Dispatcher;
133
-
134
- class ShowNews implements Controller
135
- {
136
- public function run($id) {
137
- // load and display news article...
138
- }
139
- }
140
-
141
- $router = new Router();
142
- $router->route('news/<id:int >', ShowNews::class);
143
-
144
- $dispatcher = new Dispatcher($router);
145
-
146
- $dispatcher->run('GET', '/news');
147
- ```
148
-
149
- The named parameter in this example will be converted to an integer and provided
150
- to the ` run() ` method.
151
-
152
-
153
- ### URL Creation
154
-
155
- In addition, we provide a [ base class for URL creation helpers] ( src/UrlHelper.php ) -
156
- this has no direct relationship with the router as such, it's provided for
157
- convenience, to support our overall strategy of creating testable URL helpers.
158
-
159
-
160
100
## Optimization
161
101
162
102
You can save and restore the defined routes:
@@ -218,3 +158,10 @@ after some discussion, we decided URL creation provides only a small benefit, gu
218
158
is consistent with defined patterns; but also, we value the freedom to fully customize URL creation on a
219
159
case-by-case basis using simpler code (as per [ case 3] ( https://gist.github.com/mindplay-dk/feb4768dbb118c651ba0#file-router-3-php ) )
220
160
and as such the absence of URL creation can actually be seen as a benefit.
161
+
162
+ ## Acknowledgements
163
+
164
+ Timber started as a fork of [ TreeRoute] ( https://github.com/baryshev/TreeRoute ) by
165
+ [ Vadim Baryshev] ( https://github.com/baryshev ) , the API and feature-set quickly
166
+ grew into something else entirely. What does carry over from the original fork,
167
+ is great performance.
0 commit comments