Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8bcd033

Browse files
author
Fad M.R
committed
change readme and improvement Urlgenerator class
1 parent d391739 commit 8bcd033

File tree

3 files changed

+100
-30
lines changed

3 files changed

+100
-30
lines changed

‎README.md‎

Lines changed: 96 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,107 @@
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+
33

44
## Requirements
55

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 )
1610

17-
Simple usage :
11+
**How to use ?**
1812

19-
```php
13+
```php
2014
<?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+
}
2153

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;
2480

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+
}
3385

34-
$controller = $routeMatching->getController();
35-
$action = $routeMatching->getAction();
86+
echo $controller(...array_values($arguments));
3687

88+
} catch (\Exception $exception) {
89+
header("HTTP/1.0 404 Not Found");
90+
}
3791
```
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)

‎src/Router.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(array $routes = [])
2929
{
3030
$this->routes = new \ArrayIterator();
3131
$this->urlGenerator = new UrlGenerator($this->routes);
32-
foreach (array_unique($routes) as $route) {
32+
foreach ($routes as $route) {
3333
$this->add($route);
3434
}
3535
}

‎src/UrlGenerator.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
final class UrlGenerator
88
{
99
/**
10-
* @var \ArrayIterator<Route>
10+
* @var \ArrayAccess<Route>
1111
*/
1212
private $routes;
1313

14-
public function __construct(\ArrayIterator $routes)
14+
public function __construct(\ArrayAccess $routes)
1515
{
1616
$this->routes = $routes;
1717
}
1818

1919
public function generate(string $name, array $parameters = []): string
2020
{
21-
if (array_key_exists($name, $this->routes) === false) {
21+
if ($this->routes->offsetExists($name) === false) {
2222
throw new \InvalidArgumentException(
2323
sprintf('Unknown %s name route', $name)
2424
);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /