/** * 初始化 * */ public function init() { $this->set('currentUrl', Request::currentUrl()); $this->set('refererUrl', Request::refererUrl()); $this->set('controller', Router::getController()); $this->set('action', Router::getAction()); $this->set('get', Request::getGet()); $this->set('post', Request::getPost()); }
protected function check_for_special_redirect($uri) { if(preg_match('@^/post_([0-9]{4}-[0-9]{2}-[0-9]{2})_([a-z0-9-]+)(/?)$@', $uri, $matches)) { global $container; $repository = new Jacobemerick\Web\Domain\Blog\Post\MysqlPostRepository($container['db_connection_locator']); $post = $repository->findPostByPath($matches[2]); if(!$post) { Loader::loadNew('controller', '/Error404Controller') ->activate(); } Loader::load('utility', 'Content'); $uri = Content::instance('URLSafe', "/{$post['category']}/{$post['path']}/")->activate(); } else { $post_uri = URLDecode::getPiece(1); if($post_uri !== null) { global $container; $repository = new Jacobemerick\Web\Domain\Blog\Post\MysqlPostRepository($container['db_connection_locator']); $post = $repository->findPostByPath($post_uri); if($post != false) { Loader::load('utility', 'Content'); $uri = Content::instance('URLSafe', "/{$post['category']}/{$post['path']}/")->activate(); } } } if($uri == '/search/') { if(Request::getGet('submit') == 'Submit Search' && Request::getGet('search')) { $uri .= Request::getGet('search'); $uri .= '/'; } } return $uri; }
public function start() { if (empty($this->routeType)) { //默认兼容模式 echo '1'; $this->routeType = 1; } if ($this->routeType == 1) { //r=app/controller/action形式 $route = Request::getGet('r'); $route = explode('/', $route); $len = count($route); if ($len == 1) { $this->app = $route[0]; } if ($len == 2) { $this->app = $route[0]; $this->controller = $route[1]; } if ($len == 3) { $this->app = $route[0]; $this->controller = $route[1]; $this->action = $route[2]; } } else { if ($this->routeType == 2) { //其他方式。。。 //获取出除域名和参数之外的路径 ,例如 /oneapi/index.php $script_name = Request::getScriptUrl(); //获取完整的路径,包含"?"之后的字符串/demo/index.php/group/module... ,eg :/oneapi/index.php/home/db/index $uri = Request::getRequestUri(); //去除url包含的当前文件的路径信息,只截取文件后的参数 if ($uri && @strpos($uri, $script_name, 0) !== false) { $uri = substr($uri, strlen($script_name)); ///home/db/index } else { //网址中省略引导文件名时 $script_name = dirname($_SERVER['SCRIPT_NAME']); if ($uri && @strpos($uri, $script_name, 0) !== false) { $uri = substr($uri, strlen($script_name)); } } $uri = ltrim($uri, '/'); //去除问号后面的查询字符串 if ($uri && false !== ($pos = @strrpos($uri, '?'))) { $uri = substr($uri, 0, $pos); } //分割数组 $uriInfoArray = explode('/', $uri); //'/'可以替换为其他的url分隔符 //去除后缀例如.html .json等的名称 $pathinfo = trim($_SERVER['PATH_INFO'], '/'); if ($pathinfo) { $suffix = strtolower(pathinfo($_SERVER['PATH_INFO'], PATHINFO_EXTENSION)); } if ($suffix) { Config::set('url.url_suffix', $suffix); } if ($uri && ($pos = strrpos($uri, Config::get('url.url_suffix'))) > 0) { $url = substr($url, 0, $pos); } //解析app 、controller、method和参数(放入$_GET) if (isset($uriInfoArray[0]) && $uriInfoArray[0] == true) { //获取controller及action名称 $this->app = $uriInfoArray[0]; $this->controller = isset($uriInfoArray[1]) && $uriInfoArray[1] == true ? $uriInfoArray[1] : Config::get('url.default_controller'); $this->action = isset($uriInfoArray[2]) && $uriInfoArray[2] == true ? $uriInfoArray[2] : Config::get('url.default_action'); //变量重组,将网址(URL)中的参数变量及其值赋值到$_GET全局超级变量数组中 if (($totalNum = sizeof($uriInfoArray)) > 4) { //dump($uriInfoArray); for ($i = 3; $i < $totalNum; $i += 2) { if (!isset($uriInfoArray[$i]) || !$uriInfoArray[$i] || !isset($uriInfoArray[$i + 1])) { continue; } $_GET[$uriInfoArray[$i]] = $uriInfoArray[$i + 1]; //dump($_GET); } } } } } //统一处理 if (!$this->app) { //app $this->app = Config::get('url.default_app'); } if (!$this->controller) { //控制器 $this->controller = Config::get('url.default_controller'); } if (!$this->action) { //动作 $this->action = Config::get('url.default_action'); } //定义 if (!defined('APP_NAME')) { define('APP_NAME', strtolower($this->app)); } if (!defined('CONTROLLER_NAME')) { define('CONTROLLER_NAME', ucfirst($this->controller)); } if (!defined('ACTION_NAME')) { define('ACTION_NAME', $this->action); } }
/** * 解析url * * @param string $url * @return array */ public static function parseUrl($url) { // 检查是否是当前脚本 $parseUrl = parse_url($url); // if ($parseUrl['path'] != '/' && $parseUrl['path'] != $script) { // return array('', '' , ''); // } // 二级域名判定 $sDomain = Request::getSubDomain(); $config = Imp::app()->instance('config')->get(); if (!empty($sDomain) && isset($config['subdomain'][$sDomain['0']])) { $controller = $config['subdomain'][$sDomain['0']]; } else { $controller = Request::getGet($config['controller_url_var'], $config['default_controller']); } $action = Request::getGet($config['action_url_var'], $config['default_action']); $controller = ucfirst($controller); return array($controller, $action, array()); }