|
| 1 | +# Overview |
| 2 | + |
| 3 | +Tiny router for tiny projects without MVC. |
| 4 | +Converts http://example.com/test to ./controllers/test.php |
| 5 | + |
| 6 | +# Installation |
| 7 | + |
| 8 | +You can install in using composer: |
| 9 | + |
| 10 | +`composer require mikechip/router` |
| 11 | + |
| 12 | +Or just include classes from _/src_ |
| 13 | + |
| 14 | +# Usage |
| 15 | + |
| 16 | +``` |
| 17 | +<?php |
| 18 | + require_once('vendor/autoload.php'); |
| 19 | +
|
| 20 | + /* |
| 21 | + * Directory where your controllers are located |
| 22 | + */ |
| 23 | + $controller_dir = __DIR__ . '/test_controllers'; |
| 24 | +
|
| 25 | + /* |
| 26 | + * Optional. URI that client requested. |
| 27 | + * $_SERVER['REQUEST_URI'] is used by default. |
| 28 | + * Use only if it is highly needed (for example, in ReactPHP) |
| 29 | + */ |
| 30 | + $request_uri = $_SERVER['REQUEST_URI']; |
| 31 | +
|
| 32 | + /* |
| 33 | + * Create new Router object and get result |
| 34 | + */ |
| 35 | + $router = new Mike4ip\Router\Router( $controller_dir, $request_uri ); |
| 36 | + $result = $router->getResult(); |
| 37 | +
|
| 38 | + try { |
| 39 | + /* |
| 40 | + * Run controller. |
| 41 | + * For example: if you requested /test, |
| 42 | + * it runs $controller_dir/test.php |
| 43 | + */ |
| 44 | + require( |
| 45 | + $result->getController() |
| 46 | + ); |
| 47 | + } catch(Mike4ip\Router\NotFoundException $e) { |
| 48 | + /* |
| 49 | + * If controller you need is not found |
| 50 | + */ |
| 51 | + http_response_code(404); |
| 52 | + print('Page not found'); |
| 53 | + } |
| 54 | +``` |
| 55 | + |
| 56 | +Or use simple shortcut: |
| 57 | + |
| 58 | +``` |
| 59 | +<?php |
| 60 | + require_once('vendor/autoload.php'); |
| 61 | + $controller_dir = __DIR__ . '/test_controllers'; |
| 62 | + \Mike4ip\Router\Router::autorun(); |
| 63 | +``` |
| 64 | + |
| 65 | +# Feedback |
| 66 | + |
| 67 | +You are welcome at Issues: https://github.com/mikechip/php7-router/issues |
0 commit comments