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

yipeiwu_com6年前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打开和关闭文件操作函数总结

在处理文件内容之前,通常需要建立与文件资源的连接,即打开文件。同样,结束该资源的操作后,应当关闭连接资源。所谓打开文件,实际是建立文件的各种有关信息,并使文件指针指向该文件,就可以发起输...

Linux环境下搭建php开发环境的操作步骤

本文主要记载了通过编译方式进行软件/开发环境的安装过程,其他安装方式忽略! 文章背景: 因为php和Apache等采用编译安装方式进行安装,然而编译安装方式,需要c,c++编译环境, 通...

php版微信小店API二次开发及使用示例

本文实例讲述了php版微信小店API二次开发及使用方法。分享给大家供大家参考,具体如下: 1. weixiaodian.php页面: <?php class wXd...

PHP二维数组去重实例分析

本文实例分析了PHP二维数组去重的方法。分享给大家供大家参考,具体如下: 都知道一维数组去重用系统函数 array_unique($arr) 然后今天我用到二维数组了,也想去重,百度一大...

分享一个超好用的php header下载函数

复制代码 代码如下:<?php/** * 发送文件 * * @author: legend(legendsky@hotmail.com) *...