Ovo je sav code koji imam:
Bootstrap.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'<h1>method does not exist</h1>';
return;
}
}else{
echo'<h1>base controller not found</h1>';
return;
}
}else{
echo'<h1>controller class does not exist</h1>';
return;
}
}
}
Controller.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);
}
}
}
Home.php
<?php
class Home extends Controller{
protected function Index(){
echo "home/index";
}
}
index.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();
}