I have problem with the autoload function here is the scenario:
Note: the MVC framework is not my own. I'm just using it to learn more about OOP and MVC.
First, the relevant files.
.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /phoenix/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [NE,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>
index.php:
<?php require_once('./application/LOADER.php'); ?>
application/LOADER.php:
<?php
chdir(dirname(__FILE__));
require_once('./config/func_main.php');
require_once('./conf_system.php');
session_start();
ob_start('ob_gzhandler', 6);
$load = new Boot();
$load->LOAD();
?>
conf_system.php:
<?php
$C = new stdClass;
$C->INCPATH = dirname(__FILE__) . '/';
if( ! file_exists($C->INCPATH.'conf_main.php') ) {
exit;
}
require_once($C->INCPATH.'conf_main.php');
chdir( $C->INCPATH );
?>
conf_main.php:
<?php
// Site Address Here:
$S->DOMAIN = 'site.com';
$S->SITE_URL = 'http://site.com/myMVC/';
?>
in config/func_main.php:
function __autoload($class_name) {
global $C;
require_once( $C->INCPATH.'libs/lib_'.$class_name.'.php' );
}
libs/libs_Boot.php:
class Boot {
public function __construct() {
$this->controller = $GLOBALS['C']->INCPATH . 'controllers/';
$this->request = array();
}
public function LOAD() {
$this->_parse_input();
$this->_load_controller();
$this->load_template();
}
private function _parse_input() {
/* Here is the logic to get the controller name. */
$request = explode('/', ...);
$this->request = $request[2];
}
private function _load_controller() {
require_once( $this->controller.$this->request.'.php' );
$controller = new $this->request;
}
public function load_template($name) {
global $C, $D;
require 'view/header.php';
require 'view/' . $name . '.php';
require 'view/footer.php';
}
}
controllers/index.php:
<?php $this->load_template('index');?>
In the view folder there is are only HTML files.
I know there should be an index
class but I want to use functions that are in the Boot class. For example:
public function redirect($loc, $abs=FALSE) {
global $C;
if( ! $abs && preg_match('/^http(s)?\:\/\//', $loc) ) {
$abs = TRUE;
}
if( ! $abs ) {
if( $loc{0} != '/' ) {
$loc = $C->SITE_URL.$loc;
}
}
if( ! headers_sent() ) {
header('Location: '.$loc);
}
echo '<meta http-equiv="refresh" content="0;url='.$loc.'" />';
echo '<script type="text/javascript"> self.location = "'.$loc.'"; </script>';
exit;
}
So that in controller/index.php I can write:
<?php
if (/* the user is not logged in */) {
$this->redirect('signin');
}
$this->load_template('index');
?>
Everything works finе up to a point. I get to see the view, but there is an error:
**Warning: require_once(/home/novacl/public_html/myMVC/application/libs/lib_index.php) [function.require-once]: failed to open stream: No such file or directory in /home/novacl/public_html/myMVC/application/config/func_main.php on line 6**
Line 6 is in the __autoload
function.
So why this is happening? If the index controller (controller/index.php) is changed to:
class index {
function __construct(){
$this->load_template('index');
}
}
I can't use it because load_template
is not a method of index
, it is a method of the Boot
class ..
What is happening?