• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP如何实现简单路由

php 搞代码 3年前 (2022-01-22) 27次浏览 已收录 0个评论

PHP如何实现简单路由?本文主要为大家详细介绍了一个简单的php路由类,感兴趣的小伙伴们可以参考一下。希望对大家有所帮助。

本文实例为大家分享了php编写一个简单的路由类,供大家参考,具体内容如下

<?phpnamespace cmhc\Hcrail; class Hcrail{   /**   * callback function   * @var callable   */  protected static $callback;   /**   * match string or match regexp   * @var string   */  protected static $match;   protected static $routeFound = false;   /**   * deal with get,post,head,put,delete,options,head   * @param  $method   * @param  $arguments   * @return   */  public static function __callstatic($method, $arguments)  {    self::$match = str_replace("//", "/", dirname($_SERVER['PHP_SELF']) . '/' . $arguments[0]);    self::$callback = $arguments[1];    self::dispatch();    return;  }   /**   * processing ordinary route matches   * @param string $requestUri   * @return   */  public static function normalMatch($requestUri)  {    if (self::$match == $requestUri) {      self::$routeFound = true;      call_user_func(self::$callback);    }    return;  }   /**   * processing regular route matches   * @param string $requestUri   * @return   */  public static function regexpMatch($requestUri)  {    //处理正则表达式    $regexp = self::$match;    preg_match("#$regexp#", $requestUri, $matches);    if (!empty($matches)) {      self::$routeFound = true;      call_user_func(self::$callback, $matches);    }    return;  }   /**   * dispatch route   * @return   */  public static function dispatch()  {    if (self::$routeFound) {      return ;    }    $requestUri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);    $requestMethod = $_SERVER['REQUEST_METHOD'];     if (strpos(self::$match, '(') === false) {      self::normalMatch($requestUri);    } else {      self::regexpMatch($requestUri);    }   }   /**   * Determining whether the route is found   * @return boolean   */  public static function isNotFound()  {    return !self::$routeFound;  } }

相关推荐:

thinkphp 路由规则终极详解(附伪静态)-菜鸟必看

Yii运行机制及路由详解

PHP学习MVC框架之路由

以上就是PHP如何实现2本文来源gao*daima.com搞@代#码&网6

搞gaodaima代码

简单路由的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP如何实现简单路由

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址