Skip to content

Commit

Permalink
Merge pull request #1 from ABGEO07/develope
Browse files Browse the repository at this point in the history
Develope
  • Loading branch information
ABGEO authored Mar 7, 2019
2 parents f739c49 + f844f4d commit 04bb489
Show file tree
Hide file tree
Showing 10 changed files with 394 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
.idea
var*
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# Cherry-router

## [v1.0.0](https://github.com/ABGEO07/cherry-router/releases/tag/v1.0.0 "v1.0.0") (2019-03-07)
#### The first stable version

- Package available on: `composer require cherry-project/router`;
- Router path has placeholder support;
- Router has all HTTP Methods support;
- Router has caching feature;
105 changes: 105 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,109 @@
# Cherry-Router
The Cherry-project Router

[![GitHub license](https://img.shields.io/github/license/abgeo07/cherry-router.svg)](https://github.com/ABGEO07/cherry-router/blob/master/LICENSE)

[![GitHub release](https://img.shields.io/github/release/abgeo07/cherry-router.svg)](https://github.com/ABGEO07/cherry-router/releases)

[![Packagist Version](https://img.shields.io/packagist/v/cherry-project/router.svg "Packagist Version")](https://packagist.org/packages/cherry-project/router "Packagist Version")

------------

## Including
**Install from composer** `composer require cherry-project/router`

**Include Autoloader in your main file** (Ex.: index.php)
```php
require_once __DIR__ . '/vendor/autoload.php';
```

Define application root directory
```php
define('__ROOT__', __DIR__);
```

In your application you must have **config.json** file for storing app configuration settings and you must define his location:
```php
define('CONFIG_FILE', __DIR__ . '/config/config.json');
```

**config.json** must contain path to **routes.json** and controllers directory

```json
{
"ROUTES_FILE": "config/routes.json",
"CONTROLLERS_PATH": "controllers"
}
```

Get app config parameters and define it:

```php
$config = file_get_contents(CONFIG_FILE)
or die("Unable to open config file!");

$config = json_decode($config, 1);

foreach ($config as $k => $v)
define($k, __DIR__ . '/' . $v);
```

**Notice**: This approach will be replaced in the new version :))

It's time to configure routes file

The routes file is a json file, where object key is route unique name.

Each route must have **path**, **method** and **action** keys. Homepage route example:
```json
{
"homepage": {
"path": "/",
"method": "GET",
"action": "web2hw\\DefaultController::index"
}
}
```

**Router file basic structure**
```json
{
"[RouteName]": {
"path": "[URL]",
"method": "[HTTP_Method]",
"action": "[Namespace]\\[Controller]::[Method]"
}
}
```

Definitions for router keys:
- **[RouteName]** - Route unique name;
- **path** - Route url. (Ex.: For address http://www.example.com/homepage [URL] is *homepage*);
- **method** - Route HTTP Method. Allowed all [HTTP methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods "HTTP methods");
- **action** - Route callback action. The firs part of action (before *::*) is your controller (stored in **CONTROLLERS_PATH**).
Controller is a simple [PHP Class](http://php.net/manual/en/language.oop5.php "PHP Class") where [Namespace] is his Namespace and
[Controller] Class name (Class name and class filename must have same names (Ex.: **[Controller].php**)).
The second part of action key (after ::) is controllers (class) public method;

Your route path can use **Placeholders**. Placeholder is a template of your route.

Route example with placeholder:
```json
{
"homepage": {
"path": "/hello/{name}",
"method": "GET",
"action": "Cherry\\DefaultController::sayHello"
}
}
```

There we have placeholder called **{name}** and we can get this value in controller:
```php
public function sayHello($args)
{
echo "Hello, {$args['name']}";
}
```

**2019 © Cherry-project**
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
],
"minimum-stability": "dev",
"require": {
"php": ">=5.6.0"
"php": ">=5.6.0",
"cherry-project/request": "^1.0",
"ext-json": "*"
},
"autoload": {
"psr-4": {
Expand Down
53 changes: 49 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions examples/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ROUTES_FILE": "config/routes.json",
"CONTROLLERS_PATH": "controllers"
}
7 changes: 7 additions & 0 deletions examples/config/routes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"homepage": {
"path": "/",
"method": "GET",
"action": "Cherry\\DefaultController::index"
}
}
11 changes: 11 additions & 0 deletions examples/controllers/DefaultController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Cherry;

class DefaultController
{
public function index()
{
echo 'Hello, World!';
}
}
12 changes: 12 additions & 0 deletions examples/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
//Include autoloader
require_once __DIR__ . '/../vendor/autoload.php';

define('__ROOT__', __DIR__);
define('CONFIG_FILE', __DIR__ . '/config/config.json');

//TODO: Create class for defining application config parameters
$config = file_get_contents(CONFIG_FILE)
or die("Unable to open config file!");

$config = json_decode($config, 1);

foreach ($config as $k => $v)
define($k, __DIR__ . '/' . $v);

use Cherry\Router;

$router = new Router();
Loading

0 comments on commit 04bb489

Please sign in to comment.