A simple implementation of a php framework, only implementing a simple routing layer

藏色散人
Release: 2023-04-09 17:28:02
forward
3756 people have browsed it

Recommended: "PHP Video Tutorial"

First look at the existing file directory

DOCUMENT_ROOR is /home/www directory

Then look at the contents of the entry file

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)."
"; call_user_func_array([$currentObj,$action],$params); return; ?>
Copy after login

Then create a simple controller user.php and put it in the applicaiton/controllers/ directory with the specific content As follows:


        
Copy after login

Finally test a correct controller jump and an incorrect controller jump

First test the correct process: http://192.168.1.99/user /index/xiaoming

Output content:

classname=User,action=index,params={"2":"xiaoming"} hello,xiaoming,lucky,you are arrive here!
Copy after login

Test the non-existent controller again, 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
Copy after login

The above is the detailed content of A simple implementation of a php framework, only implementing a simple routing layer. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!