Skip to content

Commit cfb3442

Browse files
committed
Updated class and bug fixed.
1 parent 949bacd commit cfb3442

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

src/Router.php

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,31 @@
1818
class Router
1919
{
2020
protected $baseFolder;
21+
2122
protected $routes = [];
2223
protected $middlewares = [];
2324
protected $groups = [];
25+
2426
protected $patterns = [
2527
'{a}' => '([^/]+)',
26-
'{d}' => '([0-9]+)',
27-
'{i}' => '([0-9]+)',
28-
'{s}' => '([a-zA-Z]+)',
29-
'{w}' => '([a-zA-Z0-9_]+)',
30-
'{u}' => '([a-zA-Z0-9_-]+)',
31-
'{*}' => '(.*)'
28+
'{d}' => '([0-9]+)',
29+
'{i}' => '([0-9]+)',
30+
'{s}' => '([a-zA-Z]+)',
31+
'{w}' => '([a-zA-Z0-9_]+)',
32+
'{u}' => '([a-zA-Z0-9_-]+)',
33+
'{*}' => '(.*)'
3234
];
35+
3336
protected $namespaces = [
34-
'middlewares' => '',
35-
'controllers' => ''
36-
];
37+
'middlewares' => '',
38+
'controllers' => ''
39+
];
40+
3741
protected $paths = [
38-
'controllers' => 'controllers',
39-
'middlewares' => 'middlewares'
42+
'controllers' => 'Controllers',
43+
'middlewares' => 'Middlewares'
4044
];
45+
4146
protected $errorCallback;
4247

4348
/**
@@ -56,8 +61,8 @@ function __construct($params = [])
5661

5762
if(isset($params['paths']) && $paths = $params['paths'])
5863
{
59-
$this->paths['controllers'] = $this->baseFolder . '/' . (isset($paths['controllers']) ? trim($paths['controllers'], '/') : $this->paths['controllers']);
60-
$this->paths['middlewares'] = $this->baseFolder . '/' . (isset($paths['middlewares']) ? trim($paths['middlewares'], '/') : $this->paths['middlewares']);
64+
$this->paths['controllers'] = (isset($paths['controllers']) ? $this->baseFolder . '/' . trim($paths['controllers'], '/') . '/' : $this->paths['controllers']);
65+
$this->paths['middlewares'] = (isset($paths['middlewares']) ? $this->baseFolder . '/' . trim($paths['middlewares'], '/') . '/' : $this->paths['middlewares']);
6166
}
6267

6368
if(isset($params['namespaces']) && $namespaces = $params['namespaces'])
@@ -180,12 +185,17 @@ public function middleware($name, $command)
180185
*/
181186
public function run()
182187
{
183-
$base = str_replace( $_SERVER['DOCUMENT_ROOT'], '', str_replace('\\', '/', getcwd()) ) . '/';
188+
$documentRoot = realpath($_SERVER['DOCUMENT_ROOT']);
189+
$getCwd = realpath(getcwd());
190+
191+
$base = str_replace('\\', '/', str_replace($documentRoot, '', $getCwd) . '/');
184192
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
185193

186194
if(($base != $uri) && (substr($uri, -1) == '/'))
187195
$uri = substr($uri, 0, (strlen($uri)-1));
188196

197+
if($uri === '') $uri = '/';
198+
189199
$method = RouterRequest::getRequestMethod();
190200

191201
$searches = array_keys($this->patterns);
@@ -394,13 +404,14 @@ private function addRoute($uri, $method, $callback, $settings)
394404
foreach ($this->groups as $key => $value)
395405
$group .= $value['route'];
396406

397-
$route = rtrim(dirname($_SERVER['PHP_SELF']) . $group . '/' . trim($uri, '/'), '/');
407+
$page = dirname($_SERVER['PHP_SELF']);
408+
$route = rtrim($page . $group . '/' . trim($uri, '/'), '/');
398409

399-
if($route == dirname($_SERVER['PHP_SELF']))
410+
if($route == $page)
400411
$route .= '/';
401412

402413
$data = [
403-
'route' => $route,
414+
'route' => $route,
404415
'method' => strtoupper($method),
405416
'callback' => (is_object($callback) ? $callback : $this->namespaces['controllers'] . $callback),
406417
'alias' => (isset($settings['alias']) ? $settings['alias'] : (isset($settings['as']) ? $settings['as'] : null)),
@@ -490,5 +501,4 @@ public function getRoutes()
490501
{
491502
return $this->routes;
492503
}
493-
494504
}

src/Router/RouterCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public static function beforeAfter($command, $middleware, $path = '', $namespace
3636
$parts = explode('/', $command);
3737
$segments = explode('@', end($parts));
3838

39-
$middlewareFile = realpath($path . strtolower(str_replace($namespace, '', $segments[0])) . '.php');
39+
$middlewareFile = realpath($path . (str_replace($namespace, '', $segments[0])) . '.php');
4040

4141
if(count($parts) > 1)
42-
$middlewareFile = realpath($path . $parts[0] . '/' . strtolower(str_replace($namespace, '', $segments[0])) .'.php');
42+
$middlewareFile = realpath($path . $parts[0] . '/' . (str_replace($namespace, '', $segments[0])) .'.php');
4343

4444
if(!file_exists($middlewareFile))
4545
return new RouterException($segments[0] . ' middleware file is not found. Please, check file.');
@@ -76,10 +76,10 @@ public static function runRoute($command, $params = null, $path = '', $namespace
7676
$parts = explode('/', $command);
7777
$segments = explode('@', end($parts));
7878

79-
$controllerFile = realpath($path . strtolower(str_replace($namespace, '', $segments[0])) . '.php');
79+
$controllerFile = realpath($path . (str_replace($namespace, '', $segments[0])) . '.php');
8080

8181
if(count($parts) > 1)
82-
$controllerFile = realpath($path . $parts[0] . '/' . strtolower($segments[0]).'.php');
82+
$controllerFile = realpath($path . $parts[0] . '/' . ($segments[0]).'.php');
8383

8484
if(!file_exists($controllerFile))
8585
return new RouterException($segments[0] . ' Controller File is not found. Please, check file.');

0 commit comments

Comments
 (0)