Skip to content

Commit

Permalink
Merge pull request #22 from tanhongit/upgrade
Browse files Browse the repository at this point in the history
Upgrade
  • Loading branch information
tanhongit authored Dec 30, 2023
2 parents 13914cc + f9bd137 commit 6a0583f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 23 deletions.
22 changes: 20 additions & 2 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
RewriteEngine On

# Redirect to HTTPS (uncomment the following lines if SSL is set up and you want to force HTTPS)
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Base directory
RewriteBase /

# Redirect the root domain to a specific controller and action (if needed)
# RewriteRule ^$ index.php?controller=home&action=index [L,QSA]

# Skip existing files from rewriting
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?controller=$1 [QSA,L]
RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]

# Rewrite for clean URLs
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?controller=$1&action=index [L,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?controller=$1&action=$2 [L,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([0-9]+)/?$ index.php?controller=$1&action=$2&id=$3 [L,QSA]

# Handle requests for the root URL
RewriteRule ^$ index.php [L,QSA]
54 changes: 35 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,32 @@ You need to change the connection information to the database if you want to sto

Path: [`/config/database.php.example`](https://github.com/tanhongit/yl-mvc-structure/tree/main/config)

Copy the file `database.php.example` to `database.php` and edit the information in the file.
Copy the file `database.php.example` to `database.php`.

```bash
cd config
cp database.php.example database.php
```

Edit the file `database.php` and change the following lines with your database information:

```php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'mvc-structure');
const DB_HOST = '127.0.0.1'; # this is ip of mysql in your local
const DB_PORT = 3306; # this is port of mysql in your local

const DB_USER = 'root';
const DB_PASSWORD = 'rootpassword';

const DB_NAME = 'mvc-structure';
```

# 5. Install and using ssl certificate
# 5. Install and using ssl certificate (optional)

Using **mkcert** to create ssl certificate for this project.

Using **mkcert** to create ssl certificate
### Install mkcert

### On Ubuntu
#### On Ubuntu

```shell
sudo apt install libnss3-tools
Expand All @@ -63,6 +75,10 @@ Now that the mkcert utility is installed, run the command below to generate and
mkcert -install
```

#### On other OS

Please refer to the instructions at the following link: [https://github.com/FiloSottile/mkcert](https://github.com/FiloSottile/mkcert)

### Create ssl certificate for this project

Run:
Expand All @@ -74,7 +90,7 @@ mkcert local.yl_mvc_structure.com

### Update configuration

Setup conf file using Apache:
Setup conf file if you are using Apache2:

Change **local.yl_mvc_structure.com.conf** file (/apache2/sites-available/ to this)

Expand Down Expand Up @@ -116,16 +132,16 @@ Change **local.yl_mvc_structure.com.conf** file (/apache2/sites-available/ to th
When done, you can test the website by opening the browser and typing the following URL:

```shell
https://local.yl_mvc_structure.com
http://localhost

https://local.yl_mvc_structure.com/product
https://local.yl_mvc_structure.com/product/all
https://local.yl_mvc_structure.com/product/show/1
https://local.yl_mvc_structure.com/category
http://localhost/product
http://localhost/product/all
http://localhost/product/show/1
http://localhost/category
...
```

> **_Note_**: To could run the above URLs, you make sure your local or your host must be enabled the `mod_rewrite` module.
> **_Note_**: If using Apache, make sure the `mod_rewrite` module is enabled.
You can also use the following command to check if the module is enabled (for Apache on Ubuntu):

Expand All @@ -136,8 +152,8 @@ sudo a2enmod rewrite
**If you can't enable the `mod_rewrite` module, you can use the following URL to run the project:**

```shell
https://local.yl_mvc_structure.com/index.php?controller=product&action=index
https://local.yl_mvc_structure.com/index.php?controller=product&action=all
https://local.yl_mvc_structure.com/index.php?controller=product&action=show&id=1
https://local.yl_mvc_structure.com/index.php?controller=category&action=index
http://localhost/index.php?controller=product&action=index
http://localhost/index.php?controller=product&action=all
http://localhost/index.php?controller=product&action=show&id=1
http://localhost/index.php?controller=category&action=index
```
11 changes: 11 additions & 0 deletions default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Custom rewrite rules for yl_mvc_structure

location / {
try_files $uri $uri/ @rewrite;
}

location @rewrite {
rewrite ^/([a-zA-Z0-9_-]+)/?$ /index.php?controller=$1&action=index last;
rewrite ^/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ /index.php?controller=$1&action=$2 last;
rewrite ^/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([0-9]+)/?$ /index.php?controller=$1&action=$2&id=$3 last;
}
9 changes: 7 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
session_start();

// Require all PHP files from a directory
$folderList = array(
$folderList = [
'./config/',
'./app/Core/',
'./lib/',
);
];
$gfgFolderList = ['Controller' => './app/Controller/'];

foreach ($folderList as $folder) {
Expand Down Expand Up @@ -35,6 +35,7 @@
closedir($gfgDir);
}
}
// ------------------------------------------------------------------------

// Get Controller
if (isset($_REQUEST['controller']) && '' != $_REQUEST['controller']) {
Expand All @@ -52,18 +53,22 @@
require $fileTemp;
}
}
// ------------------------------------------------------------------------

// Get Action
if (isset($_REQUEST['action']) && '' != $_REQUEST['action']) {
$actionParam = strtolower($_REQUEST['action']);
}
$actionName = $actionParam ?? 'Index';
// ------------------------------------------------------------------------


// 404 page for not found controller
if (!class_exists($controllerName)) {
$controllerName = 'Controller';
$actionName = 'NotFound';
}
// ------------------------------------------------------------------------

//Run
$controllerObject = new $controllerName();
Expand Down
23 changes: 23 additions & 0 deletions route.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$request = $_SERVER['REQUEST_URI'];
if (file_exists(__DIR__ . $request)) {
return false;
} else {
if (preg_match('/^\/([^\/]+)\/([^\/]+)\/([^\/]+)$/', $request, $matches)) {
$_GET['controller'] = $_REQUEST['controller'] = $matches[1];
$_GET['action'] = $_REQUEST['action'] = $matches[2];
$_GET['id'] = $_REQUEST['id'] = $matches[3];
include __DIR__ . '/index.php';
} elseif (preg_match('/^\/([^\/]+)\/([^\/]+)$/', $request, $matches)) {
$_GET['controller'] = $_REQUEST['controller'] = $matches[1];
$_GET['action'] = $_REQUEST['action'] = $matches[2];
include __DIR__ . '/index.php';
} elseif (preg_match('/^\/([^\/]+)$/', $request, $matches)) {
$_GET['controller'] = $_REQUEST['controller'] = $matches[1];
include __DIR__ . '/index.php';
} else {
header("HTTP/1.0 404 Not Found");
echo '404 Not Found';
}
}

0 comments on commit 6a0583f

Please sign in to comment.