Home >Backend Development >PHP Tutorial >Implementation code of PHP simple routing and class automatic loading function
This article mainly introduces the simple routing and class automatic loading functions implemented by PHP. It analyzes the principles and related implementation techniques of PHP routing and class automatic loading in the form of examples. Friends in need can refer to it. I hope it can help everyone. .
The project directory is as follows
Entry file index.php
##
<?php define('WEBROOT', 'C:/Users/Administrator/Documents/NetBeansProjects/test'); require_once(WEBROOT.'/core/environment.php'); core__app::run(); //Class automatic loading file environment.php
<?php //根据类名来include文件 class loader { //找到对应文件就include static function load($name) { $file = self::filepath($name); if ($file) { return include $file; } } static function filepath($name, $ext = '.php') { if (!$ext) { $ext = '.php'; } $file = str_replace('__', '/', $name) . $ext; //类名转路径 $path .= WEBROOT . '/' . $file; if (file_exists($path)) { return $path; //找到就返回 } return null; } } spl_autoload_register('loader::load');The loading rules of my class here are, for example,
core__app::run() corresponding to the
run() method of the root directory/core/app.php, The
spl_autoload_register() function is used to implement automatic loading. When a certain class name is called,
spl_autoload_register('loader::load') will be automatically executed. According to the class name include the corresponding class file.
<?php class core__app { static function run() { $a = $_SERVER['REQUEST_URI']; $uri = rtrim(preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']), '/'); $params = explode('/', trim($uri, '/')); $count = count($params); if ($count > 1) { $controller = $params[0]; $method = $params[1]; } elseif ($count == 1) { $controller = 'index'; $method = $params[0]; } else { } $filename = WEBROOT . '/controller/' . $controller . '.php'; $controller = 'controller__'.$controller; try { if (!file_exists($filename)) { throw new Exception('controller ' . $controller . ' is not exists!'); return; } include($filename); if (!class_exists($controller)) { throw new Exception('class ' . $controller . ' is not exists'); return; } $obj = new ReflectionClass($controller); if (!$obj->hasMethod($method)) { throw new Exception('method ' . $method . ' is not exists'); return; } } catch (Exception $e) { echo $e; //展示错误结果 return; } $newObj = new $controller(); call_user_func_array(array($newObj, $method), $params); } }Find the corresponding controller according to the request uri, use
call_user_func_array() method to call the method in the controller
<?php class controller__test { public function write($controller, $method) { //config__test::load('test'); model__test::write($controller, $method); } }In fact, the call here does not necessarily have to be called in the model The test method can adjust any file in the model directory. Before that, you can read some config files and other operations. Root directory/model/test.php
<?php class model__test { public function write($model, $method) { echo 'From controller:'.$model.' to model: ' . $model . ' ,method: ' . $method; } }For example, the request hostname/test/write will come in from the entry file and go through
core__app ::run will find the corresponding controller__test class under the controller and execute the
write() method
How to implement simple PHP Routing
JS method to implement simple router function
JS method to implement simple router function_javascript skills
The above is the detailed content of Implementation code of PHP simple routing and class automatic loading function. For more information, please follow other related articles on the PHP Chinese website!