Skip to content

Commit 3c23a94

Browse files
committed
Feat: added config and updated related files
1 parent 648d002 commit 3c23a94

File tree

7 files changed

+28
-13
lines changed

7 files changed

+28
-13
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ The `controller/controller.router.php` will render the current page(based on the
1212
Should something went wrong, please check if the included `.htaccess` file actually exists and `mod_rewrite` is available.<br>
1313
The `.htaccess` should also be updated accordingly if you are not using `apache` and using other kind of webservers such as `nginx`.
1414

15+
## Configuration
16+
It is advised to define the `BASE_URL` of your project from the `config.php` wether it is `http://example.com/` or
17+
`http://example.com/subfolder/`.
18+
19+
The `BASE_ROUTE` constant is also required to be configured if your project's root directory is a subfolder.
20+
For example, a project to be deployed on `http://example.com/` can have the default `BASE_ROUTE` which is `0`.
21+
22+
### Example:
23+
```
24+
`http://example.com/subfolder/` -> BASE_ROUTE = 1
25+
`http://example.com/subfolder/another/` -> BASE_ROUTE = 2
26+
```
27+
1528
## Requirements
1629
Nothing special, just your server running on `PHP v5^` (apache prefered) and `mod_rewrite` enabled.
1730

component/head.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
<div class="col">
2323
<div class="d-block position-relative p-5 text-start shadow bg-white">
2424
<p>Try the following links: </p>
25-
<a href="http://localhost/basic-php-router/home/" class="btn btn-sm btn-primary px-4 py-2 mb-2 me-1">Home Page</a>
26-
<a href="http://localhost/basic-php-router/about/" class="btn btn-sm btn-primary px-4 py-2 mb-2 me-1">About Page</a>
27-
<a href="http://localhost/basic-php-router/a-404-page-or-somethin-random/" class="btn btn-sm btn-primary px-4 py-2 mb-2 me-1">A 404 Page</a>
25+
<a href="<?= BASE_DIR ?>home/" class="btn btn-sm btn-primary px-4 py-2 mb-2 me-1">Home Page</a>
26+
<a href="<?= BASE_DIR ?>about/" class="btn btn-sm btn-primary px-4 py-2 mb-2 me-1">About Page</a>
27+
<a href="<?= BASE_DIR ?>a-404-page-or-somethin-random/" class="btn btn-sm btn-primary px-4 py-2 mb-2 me-1">A 404 Page</a>
2828
</div>
2929
</div>

config.php

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
define('BASE_DIR', 'https://php-router.herokuapp.com/');
4+
define('BASE_ROUTE', 0);

controller/controller.router.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,21 @@ class Router {
88
* Get the requested page
99
*
1010
*/
11-
public function requestPage()
12-
{
11+
public function __construct(){
1312
$page = '404';
1413
if(isset($_SERVER["QUERY_STRING"])) {
1514
$uri = explode("/", $_SERVER["QUERY_STRING"]);
16-
$page = isset($uri[0]) && !empty($uri[0]) ? $uri[0] : 'home';
15+
$page = isset($uri[BASE_ROUTE]) && !empty($uri[BASE_ROUTE]) ? $uri[BASE_ROUTE] : 'home';
1716
}
1817
$this->request = $page;
19-
return $this;
2018
}
2119

2220
/**
2321
* Render the requested page
2422
*
2523
*/
2624

27-
public function renderPage()
25+
public function init()
2826
{
2927
require_once 'include/include.import.php';
3028
switch(strtolower($this->request)) {

include/include.import.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
22

3-
$BASE_DIR = 'http://localhost/php-router';
3+
$BASE_DIR = BASE_DIR;
44
$variable = "Hello I'am a variable defined from <code>include/include.import.php</code>";

index.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* And render current view based on the URL request
66
*/
77

8-
require_once 'controller/controller.router.php';
8+
require 'config.php';
9+
require 'controller/controller.router.php';
910

1011
$router = new Router();
11-
$router->requestPage()->renderPage();
12+
$router->init();
1213

13-
/* EOF */

page/404.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<div class="col">
66
<div class="d-block position-relative p-5 text-start shadow bg-white">
7-
<p>This is a <b>404</b> page! Routes not defined on <code>controller/controller.router.php</code> <code>renderPage()</code> will redirect here!</p>
7+
<p>This is a <b>404</b> page! Routes not defined on <code>controller/controller.router.php</code> <code>init()</code> will redirect here!</p>
88
</div>
99
</div>
1010
<?php

0 commit comments

Comments
 (0)