最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

項(xiàng)目目錄如下

入口文件index.php

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

類自動(dòng)加載文件environment.php

<?php
//根據(jù)類名來(lái)include文件
class loader {
  //找到對(duì)應(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() 對(duì)應(yīng) 根目錄/core/app.php 的 run()方法,用到了spl_autoload_register()函數(shù)實(shí)現(xiàn)自動(dòng)加載,當(dāng)調(diào)用某個(gè)類名的時(shí)候,會(huì)自動(dòng)執(zhí)行spl_autoload_register('loader::load'),根據(jù)類名include對(duì)應(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; //展示錯(cuò)誤結(jié)果
      return;
    }
    $newObj = new $controller();
    call_user_func_array(array($newObj, $method), $params);
  }
}

根據(jù)請(qǐng)求uri去找對(duì)應(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è)請(qǐng)求就會(huì)從入口文件進(jìn)來(lái),經(jīng)過(guò)core__app::run就會(huì)找到controller下對(duì)應(yīng)的的controller__test類,執(zhí)行write()方法

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

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

相關(guān)文章

  • PHP eval函數(shù)使用介紹

    PHP eval函數(shù)使用介紹

    eval()函數(shù)中的eval是evaluate的簡(jiǎn)稱,這個(gè)函數(shù)的作用就是把一段字符串當(dāng)作PHP語(yǔ)句來(lái)執(zhí)行,一般情況下不建議使用容易被黑客利用
    2013-12-12
  • php實(shí)現(xiàn)讀取超大文件的方法

    php實(shí)現(xiàn)讀取超大文件的方法

    這篇文章主要介紹了php實(shí)現(xiàn)讀取超大文件的方法,需要的朋友可以參考下
    2014-07-07
  • PHP設(shè)計(jì)模式之工廠模式(Factory Pattern)的講解

    PHP設(shè)計(jì)模式之工廠模式(Factory Pattern)的講解

    今天小編就為大家分享一篇關(guān)于PHP設(shè)計(jì)模式之工廠模式(Factory Pattern)的講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-03-03
  • PHP文本操作類

    PHP文本操作類

    PHP文本操作類...
    2006-11-11
  • php將html轉(zhuǎn)成wml的WAP標(biāo)記語(yǔ)言實(shí)例

    php將html轉(zhuǎn)成wml的WAP標(biāo)記語(yǔ)言實(shí)例

    這篇文章主要介紹了php將html轉(zhuǎn)成wml的WAP標(biāo)記語(yǔ)言的方法,實(shí)例分析了php實(shí)現(xiàn)標(biāo)簽的轉(zhuǎn)換與過(guò)濾的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-07-07
  • PHP自帶方法驗(yàn)證郵箱是否存在

    PHP自帶方法驗(yàn)證郵箱是否存在

    這篇文章主要為大家詳細(xì)介紹了PHP自帶方法驗(yàn)證郵箱是否存在,以及PHP自帶方法驗(yàn)證URL、IP是否合法,感興趣的小伙伴們可以參考一下
    2016-02-02
  • php文件上傳后端處理小技巧

    php文件上傳后端處理小技巧

    這篇文章主要為大家詳細(xì)介紹了php文件上傳后端處理小技巧,幫助大家更好的進(jìn)行文件上傳操作,感興趣的朋友可以參考一下
    2016-05-05
  • php獲取apk包信息的方法

    php獲取apk包信息的方法

    這篇文章主要介紹了php獲取apk包信息的方法,非常實(shí)用的功能,需要的朋友可以參考下
    2014-08-08
  • php有道翻譯api調(diào)用方法實(shí)例

    php有道翻譯api調(diào)用方法實(shí)例

    這篇文章主要介紹了php有道翻譯api調(diào)用方法,實(shí)例分析了有道翻譯API接口的調(diào)用方法與相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-12-12
  • PHP基于redis計(jì)數(shù)器類定義與用法示例

    PHP基于redis計(jì)數(shù)器類定義與用法示例

    這篇文章主要介紹了PHP基于redis計(jì)數(shù)器類定義與用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了php定義的redis計(jì)數(shù)器類及其相關(guān)使用技巧,需要的朋友可以參考下
    2018-02-02

最新評(píng)論

仪征市| 麻江县| 滨海县| 东丽区| 青浦区| 富裕县| 千阳县| 萨嘎县| 千阳县| 临沂市| 宁都县| 牙克石市| 冕宁县| 来宾市| 宁都县| 柯坪县| 丰顺县| 上犹县| 平武县| 岢岚县| 沙雅县| 萨迦县| 冀州市| 右玉县| 广宗县| 霍州市| 大理市| 巴青县| 宁远县| 敦化市| 宽甸| 娄烦县| 沽源县| 井陉县| 西林县| 石门县| 射洪县| SHOW| 包头市| 临城县| 常宁市|