Skip to content

Commit bef4584

Browse files
committed
Json-file routes config
1 parent ca5be71 commit bef4584

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/.idea
22
/.idea_modules
33
composer.phar
4+
composer.lock
45
/vendor/
56
*.lnk
67
*.ini

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ $router
3434
->get('/api/users', IndexController::class, 'actionGetAll', 'json')
3535
->output();
3636
```
37+
Json-file with routes example:
38+
```json
39+
{
40+
"get": {
41+
"users": [
42+
"NameOfControllerWithoutControllerSuffix",
43+
"NameOfAction",
44+
"html"
45+
]
46+
}
47+
}
48+
```
3749
You can use Request static methods if you need $_GET/$_POST data in your action-method. The methods have basic data-filters for *intiger*, *email*.
3850
```php
3951
<?php

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
}
1010
],
1111
"require": {
12-
"php": ">=7.1.0"
12+
"php": ">=7.1.0",
13+
"ext-json": "*"
1314
},
1415
"require-dev": {},
1516
"autoload": {

src/AbstractRouter.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Class AbstractRouter
77
* @package Zhukmax\SimpleRouter
88
*/
9-
class AbstractRouter
9+
abstract class AbstractRouter implements RouterInterface
1010
{
1111
/** @var mixed Template engine's object */
1212
public $tplEngine;
@@ -26,6 +26,25 @@ public function __construct(array $params)
2626
if ($params['tplEngine']) {
2727
$this->tplEngine = $params['tplEngine'];
2828
}
29+
30+
if ($params['routes']) {
31+
if (is_file($params['routes'])) {
32+
$routesGroups = json_decode(file_get_contents($params['routes']), true);
33+
}
34+
35+
$this->routesFromArray($routesGroups ?? $params['routes']);
36+
}
37+
}
38+
39+
/**
40+
* @param array $routesGroups
41+
*/
42+
protected function routesFromArray(array $routesGroups)
43+
{
44+
foreach ($routesGroups as $key => $group) {
45+
$method = isset($group[2]) ? $group[2] : 'json';
46+
$this->$key(key($group), $group[0] . 'Controller', $group[1], $method);
47+
}
2948
}
3049

3150
/**

src/Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Class Router
77
* @package Zhukmax\SimpleRouter
88
*/
9-
class Router extends AbstractRouter implements RouterInterface
9+
class Router extends AbstractRouter
1010
{
1111
/**
1212
* @param string $path

0 commit comments

Comments
 (0)