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 071c6c8

Browse files
author
Fady Michel
committed
add middleware and improvement router
1 parent 30beb8f commit 071c6c8

File tree

6 files changed

+332
-10
lines changed

6 files changed

+332
-10
lines changed

‎composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
],
1818
"require": {
1919
"php": ">=7.3",
20-
"psr/http-message": "~1.0"
20+
"psr/http-message": "^1.0",
21+
"psr/http-server-middleware": "^1.0",
22+
"psr/http-factory": "^1.0"
2123
},
2224
"require-dev": {
2325
"phpunit/phpunit": "~7.1"

‎composer.lock

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

‎index.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
use LiteApp\Middleware\ControllerMiddleware;
4+
5+
require 'vendor/autoload.php';
6+
error_reporting(E_ALL);
7+
ini_set("display_errors", 1);
8+
9+
10+
class HomeController
11+
{
12+
13+
public function __invoke()
14+
{
15+
return '<p>Hello world</p>';
16+
}
17+
}
18+
19+
class ArticleController
20+
{
21+
22+
public function list()
23+
{
24+
return '<ul>
25+
<li>Article 1</li>
26+
<li>Article 2</li>
27+
</ul>';
28+
}
29+
30+
public function view(int $id)
31+
{
32+
return "<p>Article $id</p>";
33+
}
34+
}
35+
36+
37+
//$router = new \DevCoder\Router([
38+
// new \DevCoder\Route('home_page', '/', function () {
39+
// return '<p>Hello world</p>';
40+
// })
41+
//]);
42+
43+
$router = new \DevCoder\Router([
44+
new \DevCoder\Route('home_page', '/', function () {
45+
return '<p>Hello world</p>';
46+
}),
47+
// new \DevCoder\Route('home_page', '/', [HomeController::class]),
48+
new \DevCoder\Route('articles', '/articles', [ArticleController::class, 'list']),
49+
new \DevCoder\Route('article_view', '/articles/{id}', [ArticleController::class, 'view'])
50+
]);
51+
52+
try {
53+
54+
$route = $router->matchFromPath('/articles/1', 'GET');
55+
56+
$callback = $route->getCallback();
57+
$arguments = $route->getVars();
58+
59+
//
60+
if (is_array($callback)) {
61+
62+
$controller = new $callback[0]();
63+
if (is_callable($controller)) {
64+
$callback = $controller;
65+
}else {
66+
$action = $callback[1] ?? null;
67+
if (!method_exists($controller, $action)) {
68+
69+
if ($action === null) {
70+
throw new \BadMethodCallException(sprintf('Please use a Method on class %s.', get_class($controller)));
71+
}
72+
throw new \BadMethodCallException(sprintf('Method "%s" on class %s does not exist.', $action, get_class($controller)));
73+
}
74+
75+
$callback = [$controller, $action];
76+
}
77+
78+
}
79+
80+
echo $callback(...array_values($arguments));
81+
82+
} catch (\BadMethodCallException $e) {
83+
throw $e;
84+
} catch (\Exception $e) {
85+
header("HTTP/1.0 404 Not Found");
86+
}
87+
88+

‎src/Route.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Route
2121
/**
2222
* @var array<string>
2323
*/
24-
protected $controller = [];
24+
protected $parameters = [];
2525

2626
/**
2727
* @var array<string>
@@ -33,14 +33,25 @@ class Route
3333
*/
3434
protected $vars = [];
3535

36-
public function __construct(string $name, string $path, array $controller, array $methods = ['GET', 'POST'])
36+
/**
37+
* Route constructor.
38+
* @param string $name
39+
* @param string $path
40+
* @param array $parameters
41+
* $parameters = [
42+
* 0 => (string) Controller name : HomeController::class.
43+
* 1 => (string|null) Method name or null if invoke method
44+
* ]
45+
* @param array $methods
46+
*/
47+
public function __construct(string $name, string $path, array $parameters, array $methods = ['GET', 'POST'])
3748
{
3849
if ($methods === []) {
3950
throw new \InvalidArgumentException('HTTP methods argument was empty; must contain at least one method');
4051
}
4152
$this->name = $name;
4253
$this->path = $path;
43-
$this->controller = $controller;
54+
$this->parameters = $parameters;
4455
$this->methods = $methods;
4556
}
4657

@@ -74,9 +85,9 @@ public function getPath(): string
7485
return $this->path;
7586
}
7687

77-
public function getController(): array
88+
public function getParameters(): array
7889
{
79-
return $this->controller;
90+
return $this->parameters;
8091
}
8192

8293
public function getMethods(): array

0 commit comments

Comments
(0)

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