# Is my first MVC architecture set up to standards?

I just started learning about the MVC architecture. And I started to create my own MVC framework to how I like it the most.

Here I've got the index.php, indexcontroller.php and view.php. I'm not too sure if what I am doing is right, or with best practices. So I would like to know if there is anything I missed so far, or what I could improve?

Index.php (updated)

//report all php errors
error_reporting(E_ALL);

//define site root path
define('ABSPATH', dirname(__FILE__));

//include functions
foreach(glob(ABSPATH . '/functions/*.php') as $filename) { require$filename;
}

//set config array
$config = parse_ini_file(ABSPATH . '/config.ini', true);$config = json_decode(json_encode($config)); //auto load classes spl_autoload_register('autoloadCore'); spl_autoload_register('autoloadController'); spl_autoload_register('autoloadModel'); //url to class router Glue::stick((Array)$config->routes);


IndexController

class IndexController extends BaseController {
private $_view; public function GET() {$this->index();
}

public function POST() {
//don't handle post
}

public function __construct() {
$this->_view = new View(); } public function index() {$this->_view->welcome = 'Welcome!';
$this->_view->render('index'); } }  View class View { private$_vars = [];

public function __set($index,$value) {
$this->_vars[$index] = $value; } function render($fileView) {
$path = ABSPATH . '/view/' .$fileView . '.php';
if(!file_exists($path)) { throw new Exception('View not found: '.$path);
}
foreach($this->_vars as$key => $value) {$$key =$value;
}
include ($path); } }  • Best way to see if it's up to standard is to see how easy it is to add new views. – Fuhrmanator Apr 27 '14 at 23:43 ## 1 Answer MVC approaches differ between languages, platforms and frameworks. They are usually termed "MV* frameworks" because they don't exactly follow strict MVC. But we call them MVC nonetheless. # Separate core logic from configurable logic The way I understand your code, it looks like you've gone over CodeIgniter (or something similar). I assume all your requests run through index.php where the initial logic runs, like the helper loading, routing etc. For that, I suggest you separate the autoloader list and routing list to different files. You can load them via something like include. That way, you don't accidentally modify the core logic. // autoload.php$autoload = array(
);

// routes.php
$routes = array( '/' => 'Index', '/signup' => 'SignUp', '/login' => 'Login' );  Additionally, the word "Controller" might not be necessary for the routing. You already know that they always go to controllers. You might want to do that in the underlying logic instead, and keep it easy on the configurable parts. # Route lists cons One thing to note with this routing strategy is that whenever you add a controller, you always need to list down the route. This approach is not that nice, especially when you are going to be handling a hundred routes (and trust me, it ain't a walk in the park). ### Auto-route + custom routes Why not automatically look for controllers based on a predefined convention (like CodeIgniter). A route of /foobar/bam would route to FoobarController and execute the bam method. As for custom routes, you can map it like so: $routes = array(
'autobam' => 'foobar/bam // A route of /autobam executes the same as foobar/bam
);


And the flow goes like:

- Parse route
- Check for match under custom
- If match, convert to equivalent route
- Use non-custom/equivalent route for locating the controller
- If none, throw error

• @KidDiamond You have to think about scaling and extensibility the system, not just code cleanliness and efficiency. Also, you can just use require/include` and PHP arrays for routes. Saves you the effort of parsing the ini file. – Joseph Apr 27 '14 at 16:20