|
1 | 1 | # Cherry-Router |
2 | 2 | The Cherry-project Router |
3 | 3 |
|
| 4 | +[](https://github.com/ABGEO07/cherry-router/blob/master/LICENSE) |
| 5 | + |
| 6 | +[](https://github.com/ABGEO07/cherry-router/releases) |
| 7 | + |
| 8 | +[](https://packagist.org/packages/cherry-project/router "Packagist Version") |
| 9 | + |
| 10 | +------------ |
| 11 | + |
| 12 | +## Including |
| 13 | +**Install from composer** `composer require cherry-project/router` |
| 14 | + |
| 15 | +**Include Autoloader in your main file** (Ex.: index.php) |
| 16 | +```php |
| 17 | +require_once __DIR__ . '/vendor/autoload.php'; |
| 18 | +``` |
| 19 | + |
| 20 | +Define application root directory |
| 21 | +```php |
| 22 | +define('__ROOT__', __DIR__); |
| 23 | +``` |
| 24 | + |
| 25 | +In your application you must have **config.json** file for storing app configuration settings and you must define his location: |
| 26 | +```php |
| 27 | +define('CONFIG_FILE', __DIR__ . '/config/config.json'); |
| 28 | +``` |
| 29 | + |
| 30 | +**config.json** must contain path to **routes.json** and controllers directory |
| 31 | + |
| 32 | +```json |
| 33 | +{ |
| 34 | + "ROUTES_FILE": "config/routes.json", |
| 35 | + "CONTROLLERS_PATH": "controllers" |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +Get app config parameters and define it: |
| 40 | + |
| 41 | +```php |
| 42 | +$config = file_get_contents(CONFIG_FILE) |
| 43 | + or die("Unable to open config file!"); |
| 44 | + |
| 45 | +$config = json_decode($config, 1); |
| 46 | + |
| 47 | +foreach ($config as $k => $v) |
| 48 | + define($k, __DIR__ . '/' . $v); |
| 49 | +``` |
| 50 | + |
| 51 | +**Notice**: This approach will be replaced in the new version :)) |
| 52 | + |
| 53 | +It's time to configure routes file |
| 54 | + |
| 55 | +The routes file is a json file, where object key is route unique name. |
| 56 | + |
| 57 | +Each route must have **path**, **method** and **action** keys. Homepage route example: |
| 58 | +```json |
| 59 | +{ |
| 60 | + "homepage": { |
| 61 | + "path": "/", |
| 62 | + "method": "GET", |
| 63 | + "action": "web2hw\\DefaultController::index" |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +**Router file basic structure** |
| 69 | +```json |
| 70 | +{ |
| 71 | + "[RouteName]": { |
| 72 | + "path": "[URL]", |
| 73 | + "method": "[HTTP_Method]", |
| 74 | + "action": "[Namespace]\\[Controller]::[Method]" |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +Definitions for router keys: |
| 80 | +- **[RouteName]** - Route unique name; |
| 81 | +- **path** - Route url. (Ex.: For address http://www.example.com/homepage [URL] is *homepage*); |
| 82 | +- **method** - Route HTTP Method. Allowed all [HTTP methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods "HTTP methods"); |
| 83 | +- **action** - Route callback action. The firs part of action (before *::*) is your controller (stored in **CONTROLLERS_PATH**). |
| 84 | +Controller is a simple [PHP Class](http://php.net/manual/en/language.oop5.php "PHP Class") where [Namespace] is his Namespace and |
| 85 | +[Controller] Class name (Class name and class filename must have same names (Ex.: **[Controller].php**)). |
| 86 | +The second part of action key (after ::) is controllers (class) public method; |
| 87 | + |
| 88 | +Your route path can use **Placeholders**. Placeholder is a template of your route. |
| 89 | + |
| 90 | +Route example with placeholder: |
| 91 | +```json |
| 92 | +{ |
| 93 | + "homepage": { |
| 94 | + "path": "/hello/{name}", |
| 95 | + "method": "GET", |
| 96 | + "action": "Cherry\\DefaultController::sayHello" |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +There we have placeholder called **{name}** and we can get this value in controller: |
| 102 | +```php |
| 103 | +public function sayHello($args) |
| 104 | +{ |
| 105 | + echo "Hello, {$args['name']}"; |
| 106 | +} |
| 107 | +``` |
| 108 | + |
4 | 109 | **2019 © Cherry-project** |
0 commit comments