Problem with custom MVC

Hello, I’m trying to make custom yet simple MVC but I have problems, I can’t get it to work. Here is the code:

Bootstrap.php

[php]

<?php class Bootstrap{ private $controller; private $action; private $request; public function __construct($request){ $this->request=$request; if(empty($this->request['controller'])){ $this->controller='home'; }else{ $this->controller=$this->request['controller']; } if(empty($this->request['action'])) { $this->action='index'; }else{ $this->action=$this->request['action']; } } public function createController(){ if(class_exists($this->controller)){ $parents=class_parents($this->controller); if(in_array('Controller',$parents)){ if(method_exists($this->controller,$this->action)){ return new $this->controller($this->action,$this->request); }else{ echo'

method does not exist

'; return; } }else{ echo'

base controller not found

'; return; } }else{ echo'

controller class does not exist

'; return; } } } [/php] [b]Controller.php[/b] [php] <?php abstract class Controller{ protected $request; protected $action; public function __construct($request,$action){ $this->request=$request; $this->action=$action; } public function executeAction(){ return $this->{$this->action}(); } protected function returnView($viewModel,$fullview){ $view='views/'.get_class($this).'/'.$this->action.'.php'; if($fullview){ require('views/main.php'); }else{ require($view); } } } [/php] [b]Home.php[/b] [php] <?php class Home extends Controller{ protected function Index(){ echo "home/index"; } }[/php] [b]index.php[/b] [php]<?php require('classes/Bootstrap.php'); require('classes/Controller.php'); require('controllers/Home.php'); $bootstrap=new Bootstrap($_GET); $controller=$bootstrap->createController(); print_r($controller); if($controller){ $controller->executeAction(); }[/php] [b].htaccess[/b] [code]Options +FollowSymLinks RewriteEngine on RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$action=$2&id=$3 [NC,L][/code]

Please add more info on the problem. what doesn’t work / work as expected? Do you get any errors?

One of the problems is that I get no errors except the information that controller class does not exist;

Well that’s a problem ^^

How are you loading the classes? Are you using an autoloader (composer)?

I’m using require function like this

[php]

<?php require('classes/Bootstrap.php'); require('classes/Controller.php'); require('controllers/Home.php'); [/php] I have posted entire code in my first post :)

You should be using an autoloader for your classes that way you won’t have to worry about if you loading them or not.

Here’s an old autoloader that I used when I first got going with classes:

[php]// Autoload classes from “classes” directory:
function class_loader($class) {
require(‘lib/classes/’ . $class . ‘.php’);
}

spl_autoload_register(‘class_loader’);[/php]

all the Classes will have to be in the folder classes

Now as you progress with OOP you can branch off an use different folders but the all have to be under one big folder and start using namespace to avoid methods (functions) and variable conflicts.

more can be found here -> http://php.net/manual/en/function.spl-autoload-register.php

BTW If I were to guess I would say your Controller Class has no idea of your Bootstrap Class (or vice-versa).

I added the files to an empty dir and it seems to work fine, haven’t done any changes


Screenshot151.JPG

Screenshot152.JPG

[member=71845]JimL[/member]

Can you please send me your verion of my code ?
I honestly don’t know why the code is not working on my machine. :’(

Sponsor our Newsletter | Privacy Policy | Terms of Service