In general, the question is how to transfer the parameter to the controller using routing? for example, a line like this: http://example.ru/control/action/parametr using the following routing:

static function start() { // ΠΊΠΎΠ½Ρ‚Ρ€ΠΎΠ»Π»Π΅Ρ€ ΠΈ дСйствиС ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ $controller_name = 'Main'; $action_name = 'index'; $routes = explode('/', $_SERVER['REQUEST_URI']); // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ имя ΠΊΠΎΠ½Ρ‚Ρ€ΠΎΠ»Π»Π΅Ρ€Π° if ( !empty($routes[1]) ) { $controller_name = $routes[1]; } // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ имя экшСна if ( !empty($routes[2]) ) { $action_name = $routes[2]; } // добавляСм прСфиксы $model_name = 'Model_'.$controller_name; $controller_name = 'Controller_'.$controller_name; $action_name = 'action_'.$action_name; // подцСпляСм Ρ„Π°ΠΉΠ» с классом ΠΌΠΎΠ΄Π΅Π»ΠΈ $model_file = strtolower($model_name).'.php'; $model_path = "application/models/".$model_file; if(file_exists($model_path)) { include "application/models/".$model_file; } // подцСпляСм Ρ„Π°ΠΉΠ» с классом ΠΊΠΎΠ½Ρ‚Ρ€ΠΎΠ»Π»Π΅Ρ€Π° $controller_file = strtolower($controller_name).'.php'; $controller_path = "application/controllers/".$controller_file; if(file_exists($controller_path)) { include "application/controllers/".$controller_file; } else { Route::ErrorPage404(); } // создаСм ΠΊΠΎΠ½Ρ‚Ρ€ΠΎΠ»Π»Π΅Ρ€ $controller = new $controller_name; $action = $action_name; if(method_exists($controller, $action)) { // Π²Ρ‹Π·Ρ‹Π²Π°Π΅ΠΌ дСйствиС ΠΊΠΎΠ½Ρ‚Ρ€ΠΎΠ»Π»Π΅Ρ€Π° $controller->$action(); } else { Route::ErrorPage404(); } } 
  • one
    you used two levels, and the rest can be given as arguments to the method, for example, so call_user_func_array([$controller, $action], array_slice($routes, 2)); - splash58

2 answers 2

You can do the following:

 <?php $controller_name = 'Main'; $action_name = 'index'; $params = []; $routes = explode('/', $_SERVER['REQUEST_URI']); // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ имя ΠΊΠΎΠ½Ρ‚Ρ€ΠΎΠ»Π»Π΅Ρ€Π° if ( isset($routes[1]) ) { $controller_name = $routes[1]; } // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ имя экшСна if ( isset($routes[2]) ) { $action_name = $routes[2]; } // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹ if ( isset($routes[3]) ) { $params = array_slice($routes, 3); } echo $controller_name."<br />"; echo $action_name."<br />"; print_r($params); 

Then by passing the GET parameters to the / controller / action / 2016/04/24 script, you can get them in the $params array

 controller action Array ( [0] => 2016 [1] => 04 [2] => 24 ) 
  • cool, just the question is a completely different structure url - splash58
  • @ splash58, yes indeed, it is necessary to recycle - cheops
  • my comments can be used :) - splash58
  • Corrected. Yes, array_slice () came to my head too :) - cheops

Good evening! Just as you get the name of the controller and the action from the $ routes array, you need to get the value of the parameters. For your example:

 // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠ΅Ρ€Π΅Π΄Π°Π½Π½ΠΎΠ³ΠΎ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° if ( !empty($routes[3]) ) { $params= $routes[3]; } 

Similarly, you can get the remaining parameters if there are more than one, and then pass them to the controller method using the call_user_func_array () function. http://php.net/manual/ru/function.call-user-func-array.php

I also recommend looking towards different microframes, such as Slim, Silex, etc.