aboutsummaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'README.md')
-rw-r--r--README.md62
1 files changed, 62 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..0526bf6
--- /dev/null
+++ b/README.md
@@ -0,0 +1,62 @@
+# php-router
+
+Just another PHP router. Simple and fast.
+
+## Usage
+
+#### Route syntax
+
+`{}` is for enumeration of predefined strings, like: `{value1,value2,value3}`.
+
+`()` is for regular expressions, like: `([\w\d]+)`.
+
+See examples.
+
+#### Code example
+
+```
+$router = new router;
+
+// main page, returned value will be `index`
+$router->add('/', 'index');
+
+// for `/test2.html`, returned value will be `test_html id=2`
+$router->add('/test(\d).html', 'test_html id=$(1)');
+
+// for `/contacts/`, returned value will be `contacts`
+$router->add('{contacts,about}/', '${1}');
+
+// for `/section2/123/action1/`, returned value will be 'section section=section2_123 action=action1'
+$router->add('{section1,section2}/(\d+)/{action1,action2}/', 'section section=${1}_$(1) action=${2}');
+
+$route = $router->find($_SERVER['DOCUMENT_URI']);
+if ($route === false) {
+ // NOT FOUND
+}
+
+// parse value
+$route = preg_split('/ +/', $route);
+$page = $route[0];
+$input = [];
+if (count($route) > 1) {
+ for ($i = 1; $i < count($route); $i++) {
+ $var = $route[$i];
+ list($k, $v) = explode('=', $var);
+ $input[$k] = $v;
+ }
+}
+```
+
+## API
+
+`add($route, $value)` - Add route. `$value` is a string in any form. See examples below.
+
+`find($route)` - Find route. Returns compiled `$value` or `false` if not found.
+
+`dump()` - Returns internal routes tree (for serialization, e.g. for production usage).
+
+`load($tree)` - Loads routes tree, previously returned by `dump()`.
+
+## License
+
+MIT