PHP实现的简单路由和类自动加载功能

yipeiwu_com5年前PHP代码库

本文实例讲述了PHP实现的简单路由和类自动加载功能。分享给大家供大家参考,具体如下:

项目目录如下

入口文件index.php

<?php
define('WEBROOT', 'C:/Users/Administrator/Documents/NetBeansProjects/test');
require_once(WEBROOT.'/core/environment.php');
core__app::run(); //

类自动加载文件environment.php

<?php
//根据类名来include文件
class loader {
  //找到对应文件就include
  static function load($name) {
    $file = self::filepath($name);
    if ($file) {
      return include $file;
    }
  }
  static function filepath($name, $ext = '.php') {
    if (!$ext) {
      $ext = '.php';
    }
    $file = str_replace('__', '/', $name) . $ext; //类名转路径
    $path .= WEBROOT . '/' . $file;
    if (file_exists($path)) {
      return $path; //找到就返回
    }
    return null;
  }
}
spl_autoload_register('loader::load');

我这里类的加载规则是 比如core__app::run() 对应 根目录/core/app.php 的 run()方法,用到了spl_autoload_register()函数实现自动加载,当调用某个类名的时候,会自动执行spl_autoload_register('loader::load'),根据类名include对应的类文件。

app.php入口文件执行的方法开始跑框架流程

<?php
class core__app {
  static function run() {
    $a = $_SERVER['REQUEST_URI'];
    $uri = rtrim(preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']), '/');
    $params = explode('/', trim($uri, '/'));
    $count = count($params);
    if ($count > 1) {
      $controller = $params[0];
      $method = $params[1];
    } elseif ($count == 1) {
      $controller = 'index';
      $method = $params[0];
    } else {
    }
    $filename = WEBROOT . '/controller/' . $controller . '.php';
    $controller = 'controller__'.$controller;
    try {
      if (!file_exists($filename)) {
        throw new Exception('controller ' . $controller . ' is not exists!');
        return;
      }
      include($filename);
      if (!class_exists($controller)) {
        throw new Exception('class ' . $controller . ' is not exists');
        return;
      }
      $obj = new ReflectionClass($controller);
      if (!$obj->hasMethod($method)) {
        throw new Exception('method ' . $method . ' is not exists');
        return;
      }
    } catch (Exception $e) {
      echo $e; //展示错误结果
      return;
    }
    $newObj = new $controller();
    call_user_func_array(array($newObj, $method), $params);
  }
}

根据请求uri去找对应的controller, 用call_user_func_array()的方式调用controller里的方法

根目录/controller/test.php

<?php
class controller__test {
  public function write($controller, $method) {
    //config__test::load('test');
    model__test::write($controller, $method);
  }
}

这里其实调用不一定要调用model里的test方法,可以调model目录下的任意文件,在此之前可以去都读一些config文件等等操作。

根目录/model/test.php

<?php
class model__test {
  public function write($model, $method) {
    echo 'From controller:'.$model.' to model: ' . $model . ' ,method: ' . $method;
  }
}

例如hostname/test/write 这个请求就会从入口文件进来,经过core__app::run就会找到controller下对应的的controller__test类,执行write()方法

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

php对象和数组相互转换的方法

本文实例讲述了php对象和数组相互转换的方法。分享给大家供大家参考。具体分析如下: 这里定义2个php匿名对象和数组相互转换的函数,代码如下: function array2obje...

php的crc32函数使用时需要注意的问题(不然就是坑)

前几天写了一个分表程序,用的hash算法是crc32.分表的函数如下: 复制代码 代码如下:     function _getHash($username...

php+ajax实现文章自动保存的方法

本文实例讲述了php+ajax实现文章自动保存的方法。分享给大家供大家参考。具体分析如下: php+ajax文章自动保存的方法主是要方便用户,提高用户体验,我们就是用ajax把数据保存一...

PHP中文件缓存转内存缓存的方法

前言 顾名思义文件缓存转内存缓存就是将存储在文件中的数据转到内存中去,实现磁盘操作转为内存操作,这样可以大大提高数据访问速度,并能实现缓存数据的分布式部署。文件缓存与内存缓存的介绍请参考...

PHP实现对文件锁进行加锁、解锁操作的方法

本文实例讲述了PHP实现对文件锁进行加锁、解锁操作的方法。分享给大家供大家参考,具体如下: 在项目中,一般都用到日志,如数据库查询日志、访问日志、对外接口请求返回参数日志,在处理日志时简...