Skip to content

Commit

Permalink
update conditions and optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhongit committed Jan 3, 2023
1 parent 84237dc commit 45a9aca
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Path: [`/config/database.php`](https://github.com/TanHongIT/yl-mvc-structure/tre
```php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_PASSWORD', '');
define('DB_NAME', 'mvc-structure');
```

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CategoryController extends Controller
public function __construct()
{
$this->loadModel('CategoryModel');
$this->categoryModel = new CategoryModel;
$this->categoryModel = new CategoryModel();
}

public function index()
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ProductController extends Controller
public function __construct()
{
$this->loadModel('ProductModel');
$this->productModel = new ProductModel;
$this->productModel = new ProductModel();
}

public function index()
Expand Down
4 changes: 3 additions & 1 deletion app/core/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
class Database
{
protected $connection = NULL;
public $connectResult;

public function __construct()
{
$this->connectResult = $this->connect();
}

/**
Expand All @@ -16,7 +18,7 @@ public function connect()
{
// Create connection
if (!$this->connection) {
$this->connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$this->connection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$this->connection->set_charset('utf8mb4');
}
return $this->connection;
Expand Down
2 changes: 1 addition & 1 deletion app/core/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

class Model extends Database
{

public function __construct()
{
parent::__construct();
}

/**
Expand Down
4 changes: 1 addition & 3 deletions app/models/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

class BaseModel extends Model
{
protected $connectResult;

public function __construct()
{
$this->connectResult = $this->connect();
parent::__construct();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion config/database.php example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
const DB_HOST = 'localhost';
const DB_USER = 'root';
const DB_PASS = '';
const DB_PASSWORD = '';
const DB_NAME = 'mvc-structure';
14 changes: 9 additions & 5 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
<?php
session_start();

//require all PHP files from a directory
// Require all PHP files from a directory
$folderList = array(
'./config/',
'./app/core/',
'./lib/',
);
foreach ($folderList as $folder) {
foreach (glob($folder . "*.php") as $filename) {
require $filename;
if (file_exists($filename)) {
require $filename;
}
}
}

require './app/models/BaseModel.php';

spl_autoload_register(function ($className) {
require './app/controllers/' . $className . '.php';
if (file_exists('./app/controllers/' . $className . '.php')) {
require './app/controllers/' . $className . '.php';
}
});

//Controller
Expand All @@ -30,8 +34,8 @@
if (isset($_REQUEST['action']) && '' != $_REQUEST['action']) {
$actionParam = strtolower($_REQUEST['action']);
}
$actionName = $actionParam ?? 'index';
$actionName = $actionParam ?? 'Index';

//Run
$controllerObject = new $controllerName;
$controllerObject = new $controllerName();
$controllerObject->$actionName();

0 comments on commit 45a9aca

Please sign in to comment.