PHP實(shí)現(xiàn)的簡(jiǎn)單路由和類自動(dòng)加載功能
本文實(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ì)有所幫助。
- PHP命名空間和自動(dòng)加載類
- PHP中類的自動(dòng)加載的方法
- PHP命名空間與自動(dòng)加載類詳解
- 解析php類的注冊(cè)與自動(dòng)加載
- php _autoload自動(dòng)加載類與機(jī)制分析
- php類的自動(dòng)加載操作實(shí)例詳解
- php類自動(dòng)加載器實(shí)現(xiàn)方法
- PHP面向?qū)ο蟪绦蛟O(shè)計(jì)之命名空間與自動(dòng)加載類詳解
- PHP類的自動(dòng)加載機(jī)制實(shí)現(xiàn)方法分析
- PHP MVC框架中類的自動(dòng)加載機(jī)制實(shí)例分析
- PHP命名空間(Namespace)的使用詳解
- PHP類的自動(dòng)加載與命名空間用法實(shí)例分析
相關(guān)文章
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將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基于redis計(jì)數(shù)器類定義與用法示例
這篇文章主要介紹了PHP基于redis計(jì)數(shù)器類定義與用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了php定義的redis計(jì)數(shù)器類及其相關(guān)使用技巧,需要的朋友可以參考下2018-02-02

