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]