欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP實(shí)現(xiàn)的簡單路由和類自動加載功能

 更新時(shí)間:2018年03月13日 10:13:31   作者:程序分子  
這篇文章主要介紹了PHP實(shí)現(xiàn)的簡單路由和類自動加載功能,結(jié)合實(shí)例形式分析了php路由及類自動加載的原理與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP實(shí)現(xiàn)的簡單路由和類自動加載功能。分享給大家供大家參考,具體如下:

項(xiàng)目目錄如下

入口文件index.php

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

類自動加載文件environment.php

<?php
//根據(jù)類名來include文件
class loader {
  //找到對應(yīng)文件就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; //類名轉(zhuǎn)路徑
    $path .= WEBROOT . '/' . $file;
    if (file_exists($path)) {
      return $path; //找到就返回
    }
    return null;
  }
}
spl_autoload_register('loader::load');

我這里類的加載規(guī)則是 比如core__app::run() 對應(yīng) 根目錄/core/app.php 的 run()方法,用到了spl_autoload_register()函數(shù)實(shí)現(xiàn)自動加載,當(dāng)調(diào)用某個(gè)類名的時(shí)候,會自動執(zhí)行spl_autoload_register('loader::load'),根據(jù)類名include對應(yīng)的類文件。

app.php入口文件執(zhí)行的方法開始跑框架流程

<?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; //展示錯誤結(jié)果
      return;
    }
    $newObj = new $controller();
    call_user_func_array(array($newObj, $method), $params);
  }
}

根據(jù)請求uri去找對應(yīng)的controller, 用call_user_func_array()的方式調(diào)用controller里的方法

根目錄/controller/test.php

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

這里其實(shí)調(diào)用不一定要調(diào)用model里的test方法,可以調(diào)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 這個(gè)請求就會從入口文件進(jìn)來,經(jīng)過core__app::run就會找到controller下對應(yīng)的的controller__test類,執(zhí)行write()方法

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論