/** * find controller and call action **/ public static function callAction($controller, $action) { if (!$controller) { $controller = 'home'; } if (!$action) { $action = 'index'; } self::$controller = $controller; self::$action = $action; //set content type if (!headers_sent()) { header('Content-type: text/html; charset=utf-8'); } if (strpos($controller, '_') === false) { //such as 'user/action' $controller = ucfirst($controller); } else { //such as 'user_extra/action' $controllerArr = explode('_', $controller); $controllerArr = array_map('ucfirst', $controllerArr); $controller = implode('_', $controllerArr); } if (!self::controllerExists($controller)) { self::_pageNotFound(); } else { $classname = 'Controller_' . $controller; $klass = new $classname(); $methods = get_class_methods($klass); if (!in_array($action . 'Action', $methods)) { //action not found self::_pageNotFound(); } else { //action find try { //call 'before' function $klass->before(); //call action $do = $action . 'Action'; $klass->{$do}(); //call 'after' function $klass->after(); } catch (Exception $e) { self::_internalServerError($e); } } } }
/** * internal redirect to another controller/action * @param $controller string * @param $action string **/ public function callAction($controller, $action) { Kiss_Router::callAction($controller, $action); }
<?php define('APP_PATH', realpath(dirname(__FILE__) . '/../')); //set include path set_include_path(implode(PATH_SEPARATOR, array(realpath(APP_PATH), realpath(APP_PATH . '/app'), realpath(APP_PATH . '/app/Model'), realpath(APP_PATH . '/app/Library'), realpath(APP_PATH . '/app/View'), get_include_path()))); // echo APP_PATH . '/app/Library/Kiss';die; $appEnv = getenv('APP_ENV') ? getenv('APP_ENV') : 'production'; //define config path define('CONFIG_PATH', APP_PATH . '/app/Config/' . $appEnv); //initial application require APP_PATH . "/app/Library/Kiss/App.php"; Kiss_App::init($appEnv, CONFIG_PATH . '/app.ini'); Kiss_App::bootstrap(); //store config to Kiss_Registry $config = Kiss_App::getConfig(); Kiss_Registry::set('config', $config); //Resources::setConfig($config); //create router $router = new Kiss_Router(); //map "/hello/*" to Controller/Home.php::hiAction() $router->addRoute('/room/(.*)', 'room/index', array(1 => 'room_id')); //run $router->dispatch();