diff --git a/.htaccess b/.htaccess index 0d08e4a..990f2e9 100644 --- a/.htaccess +++ b/.htaccess @@ -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] \ No newline at end of file + +# 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] diff --git a/README.md b/README.md index 7a152ed..6e4690a 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: @@ -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) @@ -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): @@ -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 ``` diff --git a/default.conf b/default.conf new file mode 100644 index 0000000..7224503 --- /dev/null +++ b/default.conf @@ -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; +} diff --git a/index.php b/index.php index fb985e5..1e14445 100644 --- a/index.php +++ b/index.php @@ -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) { @@ -35,6 +35,7 @@ closedir($gfgDir); } } +// ------------------------------------------------------------------------ // Get Controller if (isset($_REQUEST['controller']) && '' != $_REQUEST['controller']) { @@ -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(); diff --git a/route.php b/route.php new file mode 100644 index 0000000..3d05615 --- /dev/null +++ b/route.php @@ -0,0 +1,23 @@ +