A
A
Andrey Boychenko2015-12-08 20:13:13
PHP
Andrey Boychenko, 2015-12-08 20:13:13

PHP router, separating frontend and backend?

Good evening! Tell me the correct or more correct way to divide the WEB application into administrative and user parts. In this implementation, in fact, 1 more router will be connected, but already for the backend part. I think this is not good at all, then tell me where to dig.
In the code, I intentionally omitted error handling, and it's too early not to suggest frameworks.

<?php
/**
 *
 */
class Route
{
  public static function start()
  {
    $actionName = 'home';
    $controllerName = 'home';

    $routes = explode( "/", $_SERVER["REQUEST_URI"] );

    if( !empty( $routes[1] ) ) :
      $controllerName = $routes[1];
    endif;

    if( !empty( $routes[2] ) ) :
      $actionName = $routes[2];
    endif;

    $modelName = strtolower( $controllerName ) . "Model";
    $controllerName = strtolower( $controllerName ) . "Controller";
    $actionName = strtolower( $actionName ) . "Action";

    if( $controllerName == "adminController" ) :
      require_once 'app/backend/check.php';
      exit();
    else :

      $modelFile = "$modelName.php";
      $modelPath = "app/models/$modelFile";

      if( file_exists( $modelPath ) ) :
        require_once $modelPath;
      endif;

      $controllerFile = "$controllerName.php";
      $controllerPath = "app/controllers/$controllerFile";

      if( file_exists( $controllerPath ) ) :
        require_once $controllerPath;
      endif;

      $controller = new $controllerName;

      if( method_exists( $controller, $actionName ) ) :
        $controller->$actionName();
      endif;
    endif;
  }

}
 ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cat Anton, 2015-12-09
@Ka4_Piton

Two controllers: one for the frontend, one for the backend.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question