Answer the question
In order to leave comments, you need to log in
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);//запускаем контроллер
}
}
Answer the question
In order to leave comments, you need to log in
$Controller = array_shift($partsURL);//вырезаем имя модуля из массива
$startController->$Action($Params);//запускаем действие
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 questionAsk a Question
731 491 924 answers to any question