|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This is a controller class. |
| 4 | + * @author sun |
| 5 | + * @date: 2018/1/11 18:43 |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Sunphp\Lib\Controls; |
| 9 | +use Sunphp\Lib\Configs\Config; |
| 10 | +use Sunphp\Lib\Exceptions\ControlException; |
| 11 | +use Sunphp\Lib\Runtimes\Data; |
| 12 | + |
| 13 | +/** |
| 14 | + * This is a controller class. |
| 15 | + */ |
| 16 | + class Control{ |
| 17 | + |
| 18 | + /** |
| 19 | + * @var string |
| 20 | + */ |
| 21 | + protected $controlName = "c"; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + protected $controlDefault = "Index"; |
| 27 | + /** |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + protected $controlPostfix = "Control"; |
| 31 | + /** |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + protected $functionName = "a"; |
| 35 | + /** |
| 36 | + * @var string |
| 37 | + */ |
| 38 | + protected $functionDefault = "index"; |
| 39 | + /** |
| 40 | + * @var string |
| 41 | + */ |
| 42 | + protected $functionPostfix = "Action"; |
| 43 | + |
| 44 | + /** |
| 45 | + * |
| 46 | + * @param null |
| 47 | + */ |
| 48 | + public function __construct() |
| 49 | + { |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + public function start() |
| 54 | + { |
| 55 | + $url_c = isset($_GET[$this->controlName]) ? $_GET[$this->controlName] : $this->controlDefault; |
| 56 | + $url_control = $url_c . $this->controlPostfix; |
| 57 | + |
| 58 | + $url_a = isset($_GET[$this->functionName]) ? $_GET[$this->functionName] : $this->functionDefault; |
| 59 | + $url_action = $url_a . $this->functionPostfix; |
| 60 | + |
| 61 | + $class_name = Config::CONTROL_ADDRESS . $url_control; |
| 62 | + $fn_name = $url_action; |
| 63 | + $params = array(); |
| 64 | + try |
| 65 | + { |
| 66 | + if (class_exists($class_name)) { |
| 67 | + if (method_exists(new $class_name, $fn_name)) { |
| 68 | + call_user_func_array(array(new $class_name, "$fn_name") , $params); |
| 69 | + } else { |
| 70 | + throw new ControlException($class_name . "->" . $fn_name. " function doesn't exist"); |
| 71 | + //If the exception is thrown, this text will not be shown |
| 72 | + } |
| 73 | + } else { |
| 74 | + throw new ControlException($class_name . " class doesn't exist"); |
| 75 | + } |
| 76 | + } |
| 77 | + catch(ControlException $e) |
| 78 | + { |
| 79 | + Data::$exception_list[] = $e->getException(); |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | + } |
0 commit comments