V
V
Vladislav2014-09-04 22:14:44
PHP
Vladislav, 2014-09-04 22:14:44

How to check for the presence of a method in the controller in my example?

There is a router

class Router{

  function __construct(){
    if($_SERVER['REQUEST_URI'] != '/'){
      $URL = trim($_SERVER['REQUEST_URI'], '/');//чистим от "/"
    }else{
      $URL = DEFAULT_MODULE;//модуль по умолчанию
    }
    $partsURL = explode('/', $URL);//делаем массив из пришедшего URL
    
    $Controller = array_shift($partsURL);//вырезаем имя модуля из массива
    
    if(count($partsURL) % 2 == 0 && file_exists(PATH_CONTROLLER_FILE.$Controller.'.controller.php')){//проверяем существование контроллера
      $arraySize = count($partsURL);//узнаем размер массива
      if($arraySize != 0){//оформляем параметры
        for ($i=0; $i < $arraySize; $i++){//оформляем параметры id/123 под вид ['id'] => ['123']
        $Params[$partsURL[$i]] = $partsURL[++$i];
        }   
      }else $Params = false;
    }else{
      $Controller = 'error404';
      $Params = false;
    }
    $fileName = 'controller_'.$Controller;//определяем название класса для функции __load()
    $startController = new $fileName($Params);//запускаем контроллер
  }	
}

Initially, I did it under a URL like /controller/param/123/param2/123, etc.
But then I realized that I need to specify another action in it ... like /controller/action/param/123/param2/123 The
question is how to write it correctly? those. I can not taste the logic ...
Can someone suggest something?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vdem, 2014-09-04
@DubecZ

$Controller = array_shift($partsURL);//вырезаем имя модуля из массива

Next add:
UPD: Then after
Do
$startController->$Action($Params);//запускаем действие

S
Sergey, 2014-09-04
Protko @Fesor

Regular expressions. Do routing on regular seasons, it will be easier and nicer. And don't write logic in the constructor. The logic should be in the methods, in the constructor only stupid assignment and initialization of values.
Find out if the required class exists, or if the class method is already a detail. You can get the arguments of the action method through reflection and call this action with values ​​bound from the URI.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question