這篇文章主要講解了“PHP url的pathinfo模式加載不同控制器的實(shí)現(xiàn)方法”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“PHP url的pathinfo模式加載不同控制器的實(shí)現(xiàn)方法”吧!
使用自動(dòng)加載和解析url的參數(shù),實(shí)現(xiàn)調(diào)用到不同的控制器,實(shí)現(xiàn)了pathinfo模式和普通的url模式
文件結(jié)構(gòu):
|--Controller
|--Index
|--Index.php
|--Application.php
Application.php
<?php class Application{ public static function main(){ header("content-type:text/html;charset=utf-8"); self::register(); self::router(); } public static function register(){ spl_autoload_register("self::loadClass"); } public static function loadClass($class){ $class=str_replace('\\', '/', $class); $class="./".$class.".php"; require_once $class; } public static function router(){ if(isset($_SERVER['PATH_INFO'])){ $pathinfo=array_filter(explode("/", $_SERVER['PATH_INFO'])); for($i=1;$i<=count($pathinfo);$i++){ $key=isset($pathinfo[$i]) ? $pathinfo[$i] : ''; $value=isset($pathinfo[$i+1]) ? $pathinfo[$i+1] :""; switch ($i) { case 1: $_GET['m']=ucfirst($key); break; case 2: $_GET['c']=ucfirst($key); break; case 3: $_GET['a']=$key; break; default: if($i>3){ if($i%2==0){ $_GET[$key]=$value; } } break; } } } $_GET['m']=!empty($_GET['m']) ? ucfirst($_GET['m']) : 'Index'; $_GET['c']=!empty($_GET['c']) ? ucfirst($_GET['c']) : 'Index'; $_GET['a']=!empty($_GET['a']) ? $_GET['a'] : 'index'; $class="\\Controller\\{$_GET['m']}\\{$_GET['c']}"; $controller=new $class; $controller->$_GET['a'](); } } Application::main();
\Controller\Index\Index.php
<?php namespace Controller\Index; use Service\User; class Index{ public function __construct(){ echo "構(gòu)造方法<br/>"; } public function index(){ new User(); print_r($_GET); } public function login(){ echo "login()"; } }
效果:
感謝各位的閱讀,以上就是“PHP url的pathinfo模式加載不同控制器的實(shí)現(xiàn)方法”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)PHP url的pathinfo模式加載不同控制器的實(shí)現(xiàn)方法這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
當(dāng)前標(biāo)題:PHPurl的pathinfo模式加載不同控制器的實(shí)現(xiàn)方法-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://jinyejixie.com/article40/jseho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、App設(shè)計(jì)、微信公眾號(hào)、網(wǎng)站導(dǎo)航、響應(yīng)式網(wǎng)站、App開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容