Jump to content

Help With My Custom MVC


ShoeLace1291

Recommended Posts

So I'm in the process of coding a VERY light MVC framework... only have run into one problem thus far.  I can't seem to get my models to load.

 

Undefined property: Test::$member in W:\wamp\www\basecmd\controllers\test.php on line 8

 

This is controllers/test.php

<?php

class Test extends Controller {
    
    function index(){
        
        $this->load->model('member');
        $data = $this->member->info();
        $this->load->view('test_view');
        
    }
}

 

This is the controller class:

 

<?php

class Controller {
    
    public $load;
    public $model;
    
    function __construct(){
        
        $this->load = new Load;
        
    }
}

 

This is the load class:

<?php

class Load {
    
    function view($filename = '', $data){
        
        if(is_array($data)){
            
            extract($data);
            
        }
        
        if(file_exists(APP_PATH.'/views/'.$filename.'.php')){
            
            include(APP_PATH.'/views/'.$filename.'.php');
            
        } else {
            
            die('Requested view could not be loaded: '.$filename);
        }
        
    }
    
    function model($filename){
        
        if(file_exists(APP_PATH.'/models/'.$filename.'.php')){
        
            include(APP_PATH.'/models/'.$filename.'.php');
            
            $this->$filename = new $filename;
            
    
        } else {
            
            die('Requested model could not be loaded: '.$filename);
            
        }
        
    }
    
    function controller($classname){
        
        if(file_exists(APP_PATH.'/controllers/'.$classname.'.php')){
            
            include(APP_PATH.'/controllers/'.$classname.'.php');
            
            $$classname = new $classname;
            
        } else {
            
            die('Requested controller could not be loaded: '.$classname);
            
        }
        
    }
    
    function system_class($classname){
        
        if(file_exists(APP_PATH.'/system/'.$classname.'.php')){
            
            include(APP_PATH.'/system/'.$classname.'.php');
            
            $this->$classname = new $classname;
            
        } else {
            
            die('Requested system class could not be loaded: '.$classname);
            
        }
        
    }    
    
}

 

And finally, my front controller(index.php)

<?php

define('APP_PATH', dirname(__FILE__));

define('APP_SUBDIRECTORY', '/basecmd');

require_once(APP_PATH.'/system/load.php');

$load = new Load;

require_once(APP_PATH.'/system/controller.php');

require_once(APP_PATH.'/controllers/test.php');

$test = new Test;

$test->index();

?>

 

Note:  My front controller is just a test for now.  I still have to come up with a way to route the URL's to the correct class/method.  Any help is appreciated!  Thanks for your time.

Link to comment
Share on other sites

Oh, sorry, forgot to include this one... members.php model

 

<?php

class Member {
    
    function info(){
        
        return array(
            'first' => 'Hello, ',
            'last' => 'World!'
        );
        
    }
    
}

 

And it is called in the Test controller.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.