|
1 |
| -# PHP-Routing |
2 |
| -A simple Router for PHP using PSR-7 message implementation |
| 1 | +#A simple Router for PHP App using PSR-7 message implementation |
| 2 | + |
3 | 3 |
|
4 | 4 | ## Requirements
|
5 | 5 |
|
6 |
| - * PHP 7.2.0+ |
7 |
| - * Enable URL rewriting on your web server |
8 |
| - * Need package for PSR-7 HTTP Message |
9 |
| - (example : guzzlehttp/psr7 ) |
10 |
| - |
11 |
| - ## Installation |
12 |
| - |
13 |
| - soon |
14 |
| - |
15 |
| - ## Usage |
| 6 | +* PHP version 7.3 |
| 7 | +* Enable URL rewriting on your web server |
| 8 | +* Need package for PSR-7 HTTP Message |
| 9 | + (example : guzzlehttp/psr7 ) |
16 | 10 |
|
17 |
| -Simple usage : |
| 11 | +**How to use ?** |
18 | 12 |
|
19 |
| -``` php |
| 13 | +```php |
20 | 14 | <?php
|
| 15 | +class IndexController { |
| 16 | + |
| 17 | + public function __invoke() |
| 18 | + { |
| 19 | + return 'Hello world!!'; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +class ArticleController { |
| 24 | + |
| 25 | + public function getAll() |
| 26 | + { |
| 27 | + // db get all post |
| 28 | + return json_encode([ |
| 29 | + ['id' => 1], |
| 30 | + ['id' => 2], |
| 31 | + ['id' => 3] |
| 32 | + ]); |
| 33 | + } |
| 34 | + |
| 35 | + public function get(int $id) |
| 36 | + { |
| 37 | + // db get post by id |
| 38 | + return json_encode(['id' => $id]); |
| 39 | + } |
| 40 | + |
| 41 | + public function put(int $id) |
| 42 | + { |
| 43 | + // db edited post by id |
| 44 | + return json_encode(['id' => $id]); |
| 45 | + } |
| 46 | + |
| 47 | + public function post() |
| 48 | + { |
| 49 | + // db create post |
| 50 | + return json_encode(['id' => 4]); |
| 51 | + } |
| 52 | +} |
21 | 53 |
|
22 |
| -use Webby\Routing\Route; |
23 |
| -use Webby\Routing\Router; |
| 54 | +$router = new \DevCoder\Router([ |
| 55 | + new \DevCoder\Route('home_page', '/', [IndexController::class]), |
| 56 | + new \DevCoder\Route('api_articles_collection', '/api/articles', [ArticleController::class, 'getAll']), |
| 57 | + new \DevCoder\Route('api_articles', '/api/articles/{id}', [ArticleController::class, 'get']), |
| 58 | +]); |
| 59 | +``` |
| 60 | +##Example |
| 61 | +$_SERVER['REQUEST_URI'] = '/api/articles/2' |
| 62 | +$_SERVER['REQUEST_METHOD'] = 'GET' |
| 63 | +```php |
| 64 | +try { |
| 65 | + // Example |
| 66 | + // \Psr\Http\Message\ServerRequestInterface |
| 67 | + //$route = $router->match(ServerRequestFactory::fromGlobals()); |
| 68 | + // OR |
| 69 | + |
| 70 | + // $_SERVER['REQUEST_URI'] = '/api/articles/2' |
| 71 | + // $_SERVER['REQUEST_METHOD'] = 'GET' |
| 72 | + $route = $router->matchFromPath($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']); |
| 73 | + |
| 74 | + $parameters = $route->getParameters(); |
| 75 | + // $arguments = ['id' => 2] |
| 76 | + $arguments = $route->getVars(); |
| 77 | + |
| 78 | + $controllerName = $parameters[0]; |
| 79 | + $methodName = $parameters[1] ?? null; |
24 | 80 |
|
25 |
| -$route = new Route('home_page', '/', HomeController::class, 'indexAction'); |
26 |
| -$router = (new Router()) |
27 |
| - ->addRoute($route); |
28 |
| - |
29 |
| - /** |
30 |
| - * @var ServerRequestInterface $request |
31 |
| - */ |
32 |
| -$routeMatching = $router->match($request); |
| 81 | + $controller = new $controllerName(); |
| 82 | + if (!is_callable($controller)) { |
| 83 | + $controller = [$controller, $methodName]; |
| 84 | + } |
33 | 85 |
|
34 |
| -$controller = $routeMatching->getController(); |
35 |
| -$action = $routeMatching->getAction(); |
| 86 | + echo $controller(...array_values($arguments)); |
36 | 87 |
|
| 88 | +} catch (\Exception $exception) { |
| 89 | + header("HTTP/1.0 404 Not Found"); |
| 90 | +} |
37 | 91 | ```
|
| 92 | +How to Define Route methods |
| 93 | +```php |
| 94 | +new \DevCoder\Route('api_articles_post', '/api/articles', [ArticleController::class, 'post'], ['POST']); |
| 95 | +new \DevCoder\Route('api_articles_put', '/api/articles/{id}', [ArticleController::class, 'put'], ['PUT']); |
| 96 | +``` |
| 97 | +Generating URLs |
| 98 | +```php |
| 99 | +echo $router->generateUri('home_page'); |
| 100 | +// / |
| 101 | +echo $router->generateUri('api_articles', ['id' => 1]); |
| 102 | +// /api/articles/1 |
| 103 | +``` |
| 104 | + |
| 105 | +Ideal for small project |
| 106 | +Simple and easy! |
| 107 | +[https://github.com/devcoder-xyz/php-router](https://github.com/devcoder-xyz/php-router) |
0 commit comments