Github for project: https://github.com/wrpd/portfolio
I have a very general understanding of MVC but haven't ever implemented it. In an effort to improve my understanding and get back into a little bit of web development I started watching and reading some tutorials and started to implement a basic MVC to redesign my personal website on.
I'm looking for critique on my current code. How can I make it more elegant? What am I misunderstanding about MVC basics? Can I make things functionally more concise (In the code itself. For variable names and such I'm keeping it purposely verbose to aid in understanding for myself)?
Specifically, I'm wondering if my project structure is okay and how to better handle routing and initializing? My routes are currently set with a routing class like so:
<?php
router::set_route('/', function(){
index_controller::create_view('index');
});
router::set_route('/about', function(){
about_controller::create_view('about');
});
router::set_route('/projects', function(){
projects_controller::create_view('projects');
});
router::set_route('/contact', function(){
contact_controller::create_view('contact');
});
?>
Is this an optimal way of routing to my pages?
My init is currently autoloading my classes and requiring my routes. Like so:
<?php
function autoload($class_name){
if (file_exists('./app/core/classes/'. $class_name . '.php')){
include './app/core/classes/'. $class_name . '.php';
} else if (file_exists('./app/controllers/' . $class_name . '.php')){
include './app/controllers/' . $class_name . '.php';
} else if (file_exists('./app/models/' . $class_name . '.php')){
include './app/models/' . $class_name . '.php';
}
}
spl_autoload_register(autoload);
require_once './app/core/routes/routes.php';
?>
Should it be a class that I instantiate in index.php or is that sufficient? I like that index.php can just contain
require_once '/path/to/init.php'
but I don't know if that's the best practice.
Essentially, I'd like to make the foundation for my personal site as solid as I can while following best practice. I've seen a lot of code examples and samples but some are quite dated and my depth of knowledge isn't deep enough to know if I'm doing things the best way that I can.