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程序设计有所帮助。

相关文章

FCKeditor添加自定义按钮

在FCKeditor目录里的fckconfig.js打开,找到FCKConfig.ToolbarSets["Default"] 这里的设置是配置功能按钮的,你需要的留下,不需要的可以删掉...

PHP使用数组实现队列

PHP中将数组当做一个栈,主要是使用array_push()和array_pop()两个系统函数来完成。入栈主要是利用array_push()函数向第一个参数的数组尾部添加一个或多个元素...

PHP各种常见经典算法总结【排序、查找、翻转等】

本文实例讲述了PHP各种常见经典算法。分享给大家供大家参考,具体如下: 冒泡排序算法 public function test() { $arr = array(43, 54...

PHP中file_exists与is_file,is_dir的区别介绍

很显然file_exists是受了asp的影响,因为asp不但有fileExists还有folderExists,driverExists,那么PHP中file_exists是什么意思呢...

php中实现字符串翻转的方法

字符串:$str = "abcdefg"; 方法一(直接使用php自带函数strrev($str)) print_r(strrev($str)); 使用for循环方式,str_split...