0
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Right now you have very little code in your question; what are you actually asking us to review? The MVC framework you made? Your routing scheme? Something else? Can you please edit your question to include what specifically you are looking for review of, and then make sure there is enough code to provide a reasonable review? \$\endgroup\$ Commented Sep 23, 2019 at 18:25

1 Answer 1

1
\$\begingroup\$

If you want to learn best practices in MVC, I would suggest you to have Slim framework. If you don't want to use an existing framework and develop your own, still, I would recommend to install it on your local and understand how they are doing. Laravel is another best, but it would be overkill for a personal site.

Check their Routing example. You are almost close in your routing, but if you want to know the best practices, they have it: http://www.slimframework.com/docs/v4/objects/routing.html

In your example, I would include init.php and routes.php inside of index.php to keep it clean and declarative. Your init may eventually grow and it would become clumsy.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for the feedback. I'll check out the Slim framework to get a better understanding! \$\endgroup\$ Commented Sep 22, 2019 at 16:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.