Empfohlen: „PHP-Video-Tutorial“
Schauen Sie sich zuerst das vorhandene Dateiverzeichnis an

DOCUMENT_ROOR ist das /home/www-Verzeichnis
Dann schauen Sie sich den Inhalt der Eintragsdatei an
<?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;
?>Dann erstellen Ein einfacher Controller user.php wird im Verzeichnis applicaiton/controllers/ abgelegt. Der spezifische Inhalt lautet wie folgt:
<?php
class User {
function __construct(){
}
public function index($name='')
{
echo 'hello,'.$name.',lucky,you are arrive here!';
}
}Testen Sie abschließend einen korrekten Controller-Sprung und einen falschen Controller-Sprung
Testen Sie zunächst den korrekten Prozess: http :/ /192.168.1.99/user/index/xiaoming
Ausgabeinhalt:
classname=User,action=index,params={"2":"xiaoming"}
hello,xiaoming,lucky,you are arrive here!Testen Sie den nicht vorhandenen Controller erneut, 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 25Das obige ist der detaillierte Inhalt vonEine einfache Implementierung eines PHP-Frameworks, das nur eine einfache Routing-Schicht implementiert. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!
So öffnen Sie eine PHP-Datei
So entfernen Sie die ersten paar Elemente eines Arrays in PHP
Was tun, wenn die PHP-Deserialisierung fehlschlägt?
So verbinden Sie PHP mit der MSSQL-Datenbank
So verbinden Sie PHP mit der MSSQL-Datenbank
So laden Sie HTML hoch
So lösen Sie verstümmelte Zeichen in PHP
So öffnen Sie PHP-Dateien auf einem Mobiltelefon