추천: "PHP Video Tutorial"
먼저 기존 파일 디렉터리를 살펴보세요

DOCUMENT_ROOR는 /home/www 디렉터리입니다
그런 다음 항목 파일의 내용을 살펴보세요
<?php
$controll_action = $_GET['_ca_'];
$params = explode('/',$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 = 'index';
unset($params[0]);
}
$filename = strtolower($controller).'.php';
$controller_path = $_SERVER['DOCUMENT_ROOT'].'/application/controllers/';
if(!file_exists($controller_path.$filename)) {
throw new Exception('controller '.$filename.' is not exists!');
return;
}
include($controller_path.$filename);
$classname = ucfirst($controller);
if(!class_exists($classname)) {
throw new Excpetion('class '.$classname.' is not exists!');
}
$reflecObj = new ReflectionClass($classname);
if(!$reflecObj->hasMethod($action)){
throw new Exception('method '.$action.' is not exists!');
}
$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='')
{
echo 'hello,'.$name.',lucky,you are arrive here!';
}
}마지막으로 올바른 컨트롤러 점프와 잘못된 컨트롤러 점프를 테스트합니다.
먼저 올바른 프로세스를 테스트합니다. 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 'Exception' with message 'controller acount.php is not exists!' in /home/www/webroot/index.php:25Stack trace:#0 {main} thrown in
/home/www/webroot/index.php on line 25위 내용은 간단한 라우팅 레이어만 구현한 PHP 프레임워크의 간단한 구현의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!