Skip to content

Commit 8053b35

Browse files
committed
Fix AbstractRouter
1 parent bef4584 commit 8053b35

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
composer.phar
44
composer.lock
55
/vendor/
6+
/tests/
67
*.lnk
78
*.ini

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require_once 'vendor/autoload.php';
2121
use ProjectName\API\Controllers\IndexController;
2222
use Twig\Environment;
2323
use Twig\Loader\FilesystemLoader;
24-
use Zhukmax\Router\Router;
24+
use Zhukmax\SimpleRouter\Router;
2525

2626
/** Add Twig Template engine **/
2727
$loader = new FilesystemLoader(__DIR__ . '/src/views');

src/AbstractRouter.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ public function __construct(array $params)
4242
protected function routesFromArray(array $routesGroups)
4343
{
4444
foreach ($routesGroups as $key => $group) {
45-
$method = isset($group[2]) ? $group[2] : 'json';
46-
$this->$key(key($group), $group[0] . 'Controller', $group[1], $method);
45+
foreach ($group as $path => $item) {
46+
$this->$key($path, $item[0] . 'Controller', $item[1]);
47+
}
4748
}
4849
}
4950

@@ -128,10 +129,11 @@ private static final function routeToRegex(string $path): string
128129
* @return array
129130
* @throws \Exception
130131
*/
131-
private final function getActiveRoute()
132+
private final function getActiveRoute(): array
132133
{
134+
$requestPath = str_replace("?".$_SERVER["QUERY_STRING"], "", $_SERVER["REQUEST_URI"]);
133135
foreach ($this->routes as $path => $route) {
134-
if (preg_match($path, $_SERVER["REDIRECT_URL"])) {
136+
if (preg_match($path, $requestPath)) {
135137
$activeRoute = $route;
136138
break;
137139
}
@@ -142,7 +144,7 @@ private final function getActiveRoute()
142144
}
143145

144146
if (!in_array(strtolower($_SERVER["REQUEST_METHOD"]), $activeRoute['method'])) {
145-
throw new \Exception("Wrong HTTP method");
147+
throw new \Exception("Wrong HTTP method: " . $activeRoute['method']);
146148
}
147149

148150
return $activeRoute;

0 commit comments

Comments
 (0)