I was reading this question about how to manage the form submission in php mvc applications.
I'm trying to create a routing system to learn more about MVC and php and I want to use RedBeanPHP as the main ORM. I'm not a master with the mvc pattern in php, so I've the following code that is supposed to instantiate the relative controller when an url is requested. Can someone show me a correct implementation without a framework, of the concept? Another doubt is about RedBean. Will be loaded on every controller if i setup it on the front controller?
<?php
declare( strict_types = 1 );
require_once __DIR__.'/vendor/autoload.php';
use \RedBeanPHP\R;
$dbh = R::setup();
class Router {
public static function init()
{
$uri = trim(parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ), '/');
if( $uri != '' ){
list( $controller, $method, $args ) = explode( '/', $uri, 3 );
$controller = ucfirst( $controller ).'Controller';
if( class_exists( $controller ) ){
if( !isset($args) ){
call_user_func([new $controller, $method]);
}
call_user_func_array( new $controller, $method, [$args] );
}
}
else{
IndexController::index();
}
}
}
class IndexController {
public static function index()
{
//include TEMPLATE_PATH.'/index.php';
echo 'Hello index';
}
}
class UserController {
public function demo()
{
//include TEMPLATE_PATH.'/test.php';
echo 'Hello demo';
}
}
?>
I'm not sure if this is the right way to apply the mvc pattern, this also because if there is a form inside the loaded view template, I'm not sure how to manage it, what is the correct <form action="" >
to set. This also because I will implement redbean with the FUSE
models to validate the data.