最近着手写一个网站,从框架到应用全部重新开发,很多代码属于重造轮子,但主要的目的就是练手,因为发现最近思维有些固化了,是得好好从头到底写个项目了!
以前写的框架中路由功能非常有限,只是实现controller与action的选择,代码很简陋,没做过滤,安全性也有问题,所以就重写了一个路由。
框架出错提示
主要功能:
- controller与action的选择
- php正则rewrite美化url
- 参数过滤
- PATH_INFO与REQUEST_URI自动选择
代码如下
class router { static public $controller = ''; static public $action = ''; static public $params = array(); static public function get() { $config = array( 'controller_default' => 'index', 'action_defalut' => 'index', 'controller_delimiter' => '/', 'action_delimiter' => '/', 'params_delimiter' => '/', 'params_kv_delimiter' => '/', 'extension_default' => '.html', ); if (false != $routerConfig = nash::config('main', 'router')) $config = array_merge($config, $routerConfig); //判断是否是GET形式 if (isset($_GET['c']) || isset($_GET['a'])) { self::$controller = isset($_GET['c']) ? self::_filter($_GET['c']) : $config['controller_default']; self::$action = isset($_GET['a']) ? self::_filter($_GET['a']) : $config['action_defalut']; foreach ($_GET as $key => $value) { if (!in_array($key, array('c', 'a'))) self::$params[$key] = self::_filter($value); } return self::_output(); } $queryString = isset($_SERVER['PATH_INFO']) ? self::_pathInfo() : self::_scriptName(); $queryString = str_replace($config['extension_default'], '', $queryString); /********配置文件有rewrte规则时,对url进行改写**********/ if (isset($config['rewrite']) && is_array($config['rewrite'])) $queryString = self::_rewrite($queryString, $config['rewrite']); /*******获取取controller******/ if (false === $pos = stripos($queryString, $config['controller_delimiter'])) { self::$controller = empty($queryString) ? $config['controller_default'] : self::_filter($queryString); self::$action = $config['action_defalut']; return self::_output(); } self::$controller = self::_filter(substr($queryString, 0, $pos)); $queryString = substr($queryString, $pos+1); /*******获取取action******/ if (false === $pos = stripos($queryString, $config['action_delimiter'])) { self::$action = empty($queryString) ? $config['action_defalut'] : self::_filter($queryString); return self::_output(); } self::$action = self::_filter(substr($queryString, 0, $pos)); $queryString = substr($queryString, $pos+1); if (empty($queryString)) return self::_output(); /*******获取取params参数******/ if (false === $pos = stripos($queryString, $config['params_kv_delimiter'])) { return self::_output(); } //当params_kv_delimiter与params_delimiter两个分界符相同时 if ($config['params_kv_delimiter'] == $config['params_delimiter']) { $array = explode($config['params_delimiter'], $queryString); $count = count($array); for ($i = 0; $i < $count; $i++) { if ($i%2 == 0) { self::$params[$array[$i]] = isset($array[$i+1]) && !empty($array[$i+1]) ? self::_filter($array[$i+1]) : ''; } } return self::_output(); } //当params_kv_delimiter与params_delimiter两个分界符不同 if (false === $pos = stripos($queryString, $config['params_delimiter'])) { $array = explode($config['params_kv_delimiter'], $queryString); !empty($array[1]) && self::$params[$array[0]] = self::_filter($array[1]); return self::_output(); } $array = explode($config['params_delimiter'], $queryString); foreach ($array as $value) { if (!empty($value)) { $arrayTmp = explode($config['params_kv_delimiter'], $value); !empty($arrayTmp[1]) && self::$params[$arrayTmp[0]] = self::_filter($arrayTmp[1]); } } return self::_output(); } /** * php实现的正则rewrite url */ static private function _rewrite($string, $regular) { return preg_replace (array_keys($regular), array_values($regular), $string); } /** * 对参数进行过滤,防止注入攻击 */ static private function _filter($value) { return filter::vars($value, '#s_z'); } static private function _pathInfo() { $queryString = $_SERVER['PATH_INFO'] == '/' ? $_SERVER['PATH_INFO'] = '' : $_SERVER['PATH_INFO']; substr($queryString, 0, 1) == '/' && $queryString = substr($queryString, 1); return $queryString; } static private function _scriptName() { return substr(str_replace($_SERVER['SCRIPT_NAME'], '',$_SERVER['REQUEST_URI']),1); } static private function _output() { return array(self::$controller, self::$action, self::$params); } } //$config = array('rewrite' => array('/user\/login\/(.*)/' => "user/login/url_$1")); //var_dump(router::get());
路过,瞧瞧,呵呵。
你好已经做好了贵站的链接,请您也做好我的小站点的链接,谢谢啊http://www.ivz.com.cn
好的,我现在加上