Hello Daniweb community,
I have an app that is built on custom MVC design pattern. The app is designed that when the controller is empty, that is when $url[0] is empty, then the app should automatically redirect to index. The code to do this is found on bootstrap.php and looks as follows:
function __construct() {
$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url, '/');
$url = explode('/', $url);
//print_r($url);
if (empty($url[0])) {
require 'controllers/index.php';
$controller = new Index();
$controller->index();
return false;
}
In the same app i also have an index controller, and Index model and a base controller (that loads the base Model).
The index controller code snippet that causes the error looks like this:
<?php
class Index extends Controller {
public function __construct(){
parent::__construct();
}
public function Index(){
$this->view->userList = $this->model->userList();
$this->view->render('index/index');
echo 'INDEX INDEX INDEX';
}
}
The index model looks like this:
<?php
class Index_Model extends Model{
public function __construct(){
Parent::__construct();
}
public function userList(){
$sth = $this->db->prepare("SELECT * FROM user WHERE
year = 2018 AND month = 'February'");
//var_dump($sth);
$sth->execute();
return $sth->fetchAll();
}
}
and finally the base controller that loads the model looks like this:
<?php
class Controller {
function __construct() {
$this->view = new Views();
}
function loadModel($name){
$path ='Models/'.$name.'_model.php';
if(file_exists($path)) {
require $path;
$modelname = $name . '_Model';
$this->model = new $modelname();
}
}
}
I have been trying to figure out what might be causing this error with no much success. Most of the answers on the internet suggest a var_dump of $this->model which in my case returns a null. My question is why is this running when the url is http://localhost/Test/index but gives me an error when the url is http://localhost/Test and yet the code on the bootstrap explicitly says that when $url[0] is empty reroute to index controller?