간단한 라우팅 레이어만 구현한 PHP 프레임워크의 간단한 구현

藏色散人
풀어 주다: 2023-04-09 17:28:02
앞으로
3801명이 탐색했습니다.

추천: "PHP Video Tutorial"

먼저 기존 파일 디렉터리를 살펴보세요

DOCUMENT_ROOR는 /home/www 디렉터리입니다

그런 다음 항목 파일의 내용을 살펴보세요

<?php
	$controll_action = $_GET[&#39;_ca_&#39;];
	$params  = explode(&#39;/&#39;,$controll_action);
	$params_count = count($params);

	$otherParams = $params;
	if($params_count>1) {
		$controller = $params[0];
		$action  = $params[1];
		unset($params[0]);
		unset($params[1]);
	}else if($params_count==1) {
		$controller = $params[0];
		$action = &#39;index&#39;;
		unset($params[0]);
	}

	$filename = strtolower($controller).&#39;.php&#39;;
	$controller_path = $_SERVER[&#39;DOCUMENT_ROOT&#39;].&#39;/application/controllers/&#39;;

	if(!file_exists($controller_path.$filename)) {
		throw new Exception(&#39;controller &#39;.$filename.&#39; is not exists!&#39;);
		return;
	}
	include($controller_path.$filename);

	$classname = ucfirst($controller);
	if(!class_exists($classname)) {
		throw new Excpetion(&#39;class &#39;.$classname.&#39; is not exists!&#39;);
	}
	$reflecObj = new ReflectionClass($classname);
	if(!$reflecObj->hasMethod($action)){
		throw new Exception(&#39;method &#39;.$action.&#39; is not exists!&#39;);
	}

	$currentObj = new $classname();
	echo "classname=$classname,action=$action,params=".json_encode($params)."<br/>";
	call_user_func_array([$currentObj,$action],$params);
	return;
?>
로그인 후 복사

그런 다음 생성하세요 간단한 컨트롤러 user.php는 applicaiton/controllers/ 디렉토리에 있습니다. 구체적인 내용은 다음과 같습니다.

<?php
class User {
    
    function __construct(){
        
    }
    public function index($name=&#39;&#39;)
    {
    	echo &#39;hello,&#39;.$name.&#39;,lucky,you are arrive here!&#39;;
    }
}
로그인 후 복사

마지막으로 올바른 컨트롤러 점프와 잘못된 컨트롤러 점프를 테스트합니다.

먼저 올바른 프로세스를 테스트합니다. http :/ /192.168.1.99/user/index/xiaoming

출력 내용:

classname=User,action=index,params={"2":"xiaoming"}
 hello,xiaoming,lucky,you are arrive here!
로그인 후 복사

존재하지 않는 컨트롤러를 다시 테스트하세요. http://192.168.1.99/account/index/xiaoming

Fatal error: Uncaught exception &#39;Exception&#39; with message &#39;controller acount.php is not exists!&#39; in /home/www/webroot/index.php:25Stack trace:#0 {main} thrown in 
/home/www/webroot/index.php on line 25
로그인 후 복사

위 내용은 간단한 라우팅 레이어만 구현한 PHP 프레임워크의 간단한 구현의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
php
원천:csdn.net
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!