File tree 7 files changed +28
-13
lines changed
7 files changed +28
-13
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,19 @@ The `controller/controller.router.php` will render the current page(based on the
12
12
Should something went wrong, please check if the included ` .htaccess ` file actually exists and ` mod_rewrite ` is available.<br >
13
13
The ` .htaccess ` should also be updated accordingly if you are not using ` apache ` and using other kind of webservers such as ` nginx ` .
14
14
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
+
15
28
## Requirements
16
29
Nothing special, just your server running on ` PHP v5^ ` (apache prefered) and ` mod_rewrite ` enabled.
17
30
Original file line number Diff line number Diff line change 22
22
<div class="col">
23
23
<div class="d-block position-relative p-5 text-start shadow bg-white">
24
24
<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>
28
28
</div>
29
29
</div>
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ define ('BASE_DIR ' , 'https://php-router.herokuapp.com/ ' );
4
+ define ('BASE_ROUTE ' , 0 );
Original file line number Diff line number Diff line change @@ -8,23 +8,21 @@ class Router {
8
8
* Get the requested page
9
9
*
10
10
*/
11
- public function requestPage ()
12
- {
11
+ public function __construct (){
13
12
$ page = '404 ' ;
14
13
if (isset ($ _SERVER ["QUERY_STRING " ])) {
15
14
$ 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 ' ;
17
16
}
18
17
$ this ->request = $ page ;
19
- return $ this ;
20
18
}
21
19
22
20
/**
23
21
* Render the requested page
24
22
*
25
23
*/
26
24
27
- public function renderPage ()
25
+ public function init ()
28
26
{
29
27
require_once 'include/include.import.php ' ;
30
28
switch (strtolower ($ this ->request )) {
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
- $ BASE_DIR = ' http://localhost/php-router ' ;
3
+ $ BASE_DIR = BASE_DIR ;
4
4
$ variable = "Hello I'am a variable defined from <code>include/include.import.php</code> " ;
Original file line number Diff line number Diff line change 5
5
* And render current view based on the URL request
6
6
*/
7
7
8
- require_once 'controller/controller.router.php ' ;
8
+ require 'config.php ' ;
9
+ require 'controller/controller.router.php ' ;
9
10
10
11
$ router = new Router ();
11
- $ router ->requestPage ()-> renderPage ();
12
+ $ router ->init ();
12
13
13
- /* EOF */
Original file line number Diff line number Diff line change 4
4
5
5
<div class="col">
6
6
<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>
8
8
</div>
9
9
</div>
10
10
<?php
You can’t perform that action at this time.
0 commit comments