Skip to content

Commit 04bb489

Browse files
authored
Merge pull request #1 from ABGEO07/develope
Develope
2 parents f739c49 + f844f4d commit 04bb489

File tree

10 files changed

+394
-7
lines changed

10 files changed

+394
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
vendor
22
.idea
3+
var*

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
11
# Cherry-router
2+
3+
## [v1.0.0](https://github.com/ABGEO07/cherry-router/releases/tag/v1.0.0 "v1.0.0") (2019-03-07)
4+
#### The first stable version
5+
6+
- Package available on: `composer require cherry-project/router`;
7+
- Router path has placeholder support;
8+
- Router has all HTTP Methods support;
9+
- Router has caching feature;

README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,109 @@
11
# Cherry-Router
22
The Cherry-project Router
33

4+
[![GitHub license](https://img.shields.io/github/license/abgeo07/cherry-router.svg)](https://github.com/ABGEO07/cherry-router/blob/master/LICENSE)
5+
6+
[![GitHub release](https://img.shields.io/github/release/abgeo07/cherry-router.svg)](https://github.com/ABGEO07/cherry-router/releases)
7+
8+
[![Packagist Version](https://img.shields.io/packagist/v/cherry-project/router.svg "Packagist Version")](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+
4109
**2019 © Cherry-project**

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
],
1616
"minimum-stability": "dev",
1717
"require": {
18-
"php": ">=5.6.0"
18+
"php": ">=5.6.0",
19+
"cherry-project/request": "^1.0",
20+
"ext-json": "*"
1921
},
2022
"autoload": {
2123
"psr-4": {

composer.lock

Lines changed: 49 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/config/config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"ROUTES_FILE": "config/routes.json",
3+
"CONTROLLERS_PATH": "controllers"
4+
}

examples/config/routes.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"homepage": {
3+
"path": "/",
4+
"method": "GET",
5+
"action": "Cherry\\DefaultController::index"
6+
}
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Cherry;
4+
5+
class DefaultController
6+
{
7+
public function index()
8+
{
9+
echo 'Hello, World!';
10+
}
11+
}

examples/test.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
//Include autoloader
44
require_once __DIR__ . '/../vendor/autoload.php';
55

6+
define('__ROOT__', __DIR__);
7+
define('CONFIG_FILE', __DIR__ . '/config/config.json');
8+
9+
//TODO: Create class for defining application config parameters
10+
$config = file_get_contents(CONFIG_FILE)
11+
or die("Unable to open config file!");
12+
13+
$config = json_decode($config, 1);
14+
15+
foreach ($config as $k => $v)
16+
define($k, __DIR__ . '/' . $v);
17+
618
use Cherry\Router;
719

820
$router = new Router();

0 commit comments

Comments
 (0)