Skip to content

Commit 4560a90

Browse files
committed
Remove routing hacks
1 parent 8ebe226 commit 4560a90

File tree

6 files changed

+35
-25
lines changed

6 files changed

+35
-25
lines changed

autoload.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

3-
if ( !file_exists($autoload_file = __DIR__ . '/vendor/autoload.php') ) {
3+
if ( !file_exists($autoload_file = MPG_ABS_PATH . '/vendor/autoload.php') ) {
44
die('Install dependencies with `composer install` to run this script successfully.');
55
}
66

77
$loader = require_once $autoload_file;
88

9-
$loader->add('Controllers', __DIR__ . '/src');
10-
$loader->add('Helpers', __DIR__ . '/src');
11-
$loader->add('Normalizers', __DIR__ . '/src');
12-
$loader->add('Responses', __DIR__ . '/src');
9+
$loader->add('Controllers', MPG_ABS_PATH . '/src');
10+
$loader->add('Helpers', MPG_ABS_PATH . '/src');
11+
$loader->add('Normalizers', MPG_ABS_PATH . '/src');
12+
$loader->add('Responses', MPG_ABS_PATH . '/src');

index.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @var string
2020
*/
21-
define('MPG_APP_VERSION', '1.1.2');
21+
define('MPG_APP_VERSION', '1.1.3');
2222

2323
/**
2424
* Development mode?
@@ -31,39 +31,58 @@
3131
* Absolute path. XXX Without trailing slash.
3232
*
3333
* @var string
34+
* @example /opt/mongodb-php-gui
3435
*/
3536
define('MPG_ABS_PATH', __DIR__);
3637

3738
$baseUrl = '//' . $_SERVER['HTTP_HOST'];
38-
$serverPath = str_replace('\\', '/', dirname($_SERVER['REQUEST_URI']));
39-
$serverPath = ( $serverPath === '/' ) ? '' : $serverPath;
39+
40+
// If request matches a folder. For example: /mongo/
41+
if ( preg_match('#/$#', $_SERVER['REQUEST_URI']) ) {
42+
43+
$serverPath = $_SERVER['REQUEST_URI'];
44+
45+
} else {
46+
47+
$serverPath = dirname($_SERVER['REQUEST_URI']);
48+
49+
// Normalize directory separator in server path.
50+
if ( DIRECTORY_SEPARATOR !== '/' ) {
51+
$serverPath = str_replace(DIRECTORY_SEPARATOR, '/', $serverPath);
52+
}
53+
54+
}
55+
56+
$serverPath = rtrim($serverPath, '/');
57+
4058
$baseUrl .= $serverPath;
4159

4260
/**
4361
* Server path. XXX Without trailing slash.
4462
*
4563
* @var string
64+
* @example /mongo
4665
*/
4766
define('MPG_SERVER_PATH', $serverPath);
4867

4968
/**
5069
* Base URL. XXX Without trailing slash.
5170
*
5271
* @var string
72+
* @example http://127.0.0.1:5000/mongo
5373
*/
5474
define('MPG_BASE_URL', $baseUrl);
5575

56-
require __DIR__ . '/autoload.php';
57-
require __DIR__ . '/routes.php';
76+
require MPG_ABS_PATH . '/autoload.php';
77+
require MPG_ABS_PATH . '/routes.php';
5878

5979
$application = new Application($router);
6080
$serverRequest = ServerRequestFactory::createFromGlobals();
6181

62-
// XXX This hack makes index to work in sub-folder case.
6382
try {
6483
$response = $application->dispatch($serverRequest);
6584
} catch (NotFoundHttpException $e) {
66-
header('Location: ' . rtrim($_SERVER['REQUEST_URI'], '/') . '/index');
85+
die('Route not found. Try to append a slash to URL.');
6786
}
6887

6988
$application->send($response);

routes.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,7 @@
99

1010
$router = new Router();
1111

12-
$router->get('/', function() {
13-
14-
LoginController::ensureUserIsLogged();
15-
16-
Controller::redirectTo('/queryDatabase');
17-
18-
});
19-
20-
// XXX This hack makes index to work in sub-folder case.
21-
$router->get(MPG_SERVER_PATH . '/index', function() {
12+
$router->get(MPG_SERVER_PATH . '/', function() {
2213

2314
LoginController::ensureUserIsLogged();
2415

src/Controllers/LoginController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function renderViewAction() : Response {
6565
} else {
6666

6767
$_SESSION['mpg']['user_is_logged'] = true;
68-
Controller::redirectTo('/index');
68+
Controller::redirectTo('/');
6969

7070
}
7171

views/login.tpl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
77

8-
<title><?php echo MPG_APP_NAME . ' v' . MPG_APP_VERSION; ?></title>
8+
<title><?php echo MPG_APP_NAME; ?></title>
99

1010
<link rel="stylesheet" href="<?php echo MPG_BASE_URL; ?>/static/css/font-awesome.min.css">
1111
<link rel="stylesheet" href="<?php echo MPG_BASE_URL; ?>/static/css/bootstrap.min.css">

views/parts/menu.tpl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<nav class="navbar sticky-top navbar-dark bg-mongodb">
22

3-
<a class="navbar-brand" href="<?php echo MPG_BASE_URL; ?>/index"><?php echo MPG_APP_NAME; ?></a>
3+
<a class="navbar-brand" href="<?php echo MPG_BASE_URL; ?>/"><?php echo MPG_APP_NAME; ?></a>
44

55
<div class="navbar-nav">
66
<a class="nav-item nav-link<?php echo ('collection' === $viewName) ? ' active' : ''; ?>" data-canonical-url="<?php echo MPG_BASE_URL; ?>/manageCollections" href="<?php echo MPG_BASE_URL; ?>/manageCollections">Manage collections</a>

0 commit comments

Comments
 (0)