0

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.

Oshione
  • 88
  • 13
  • Persistence abstraction (that's what ORM would be, when used properly, not a "model") would best be initialized on demand. The clean way to do that would be by using some DI Container library. Also, your routing is terrible. And php class names are not case sensitive, and since 5.3 we have namespaces so adding suffix is pointless. And static methods are root of all evil. – tereško Jul 05 '19 at 09:59
  • @tereško I'm using the add of the word `Controller` and the `ucfirst()` function because I'm learning how to create a simple MVC and every article I've read about, use this naming convention. To be honest I don't like this way to call my controllers classes, I prefer to use a standard lowercase first letter without the suffix. About the routing, how I can improve it, do you have any suggestion? Why static methods are evil? For the DI, any suggestion, I should inject redbean for every controller instance? – Oshione Jul 05 '19 at 10:08
  • Since you are using an ORM of active record pattern, the RedBean would be a dependency for service layer classes, not controllers. For routing [this](https://stackoverflow.com/a/19309893/727208) might help (though, you probably should use a 3rd part library for that - FastRoute and Symfony's standalone Routing Component are the popular options). As for DIC ... emmm ... I have only worked with Auryn and the symfony's "Service Container" standalone component. – tereško Jul 05 '19 at 10:23
  • I will take a look at your suggestion for the routing. I want to write my own system as a learning exercise, but FastRoute is a nice solution. I will look on github for a good dic system – Oshione Jul 05 '19 at 10:33

0 Answers0