PHP實(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)文章
php 字符過濾類,用于過濾各類用戶輸入的數(shù)據(jù)
最近老看到有人的網(wǎng)站被掛馬,發(fā)一個(gè)php的字符過濾類,建議廣大站長朋友們多關(guān)注下,安全方面的知識。2009-05-05php基于curl主動推送最新內(nèi)容給百度收錄的方法
這篇文章主要介紹了php基于curl主動推送最新內(nèi)容給百度收錄的方法,分析了百度鏈接的提交方式及curl主動推送的實(shí)現(xiàn)方法,需要的朋友可以參考下2016-10-10- 現(xiàn)在很多網(wǎng)站都存在跨站腳本攻擊漏洞,讓黑客有機(jī)可乘.跨站攻擊很容易就可以構(gòu)造,而且非常隱蔽,不易被查覺(通常盜取信息后馬上跳轉(zhuǎn)回原頁面)。如何攻擊,在此不作介紹,主要談?wù)勅绾畏婪丁?/div> 2015-09-09
PHP字符轉(zhuǎn)義相關(guān)函數(shù)小結(jié)(php下的轉(zhuǎn)義字符串)
PHP字符轉(zhuǎn)義相關(guān)函數(shù)小結(jié),有時(shí)候?yàn)榱税踩鹨?,我們需要對用戶輸入的字符串進(jìn)行轉(zhuǎn)義2007-04-04PHP合并數(shù)組+與array_merge的區(qū)別分析
PHP中兩個(gè)數(shù)組合并可以使用+或者array_merge,但之間還是有區(qū)別的,而且這些區(qū)別如果了解不清楚項(xiàng)目中會要命的!2010-08-08php實(shí)現(xiàn)轉(zhuǎn)換ubb代碼的方法
這篇文章主要介紹了php實(shí)現(xiàn)轉(zhuǎn)換ubb代碼的方法,涉及php正則替換的使用技巧,需要的朋友可以參考下2015-06-06最新評論