PHP implements simple routing and automatic class loading, and php implements route loading.
This example describes the simple routing and automatic class loading functions implemented by PHP. We will share this with you for your reference. The details are as follows:
The project directory is as follows:
Entry file index. php
<?phpdefine('WEBROOT', 'C:/Users/Administrator/Documents/NetBeansProjects/test');require_once(WEBROOT.'/core/environment.php');core__app::run(); //
Class automatically loads the file environment. php
<? Php // include the file class loader according to the class name {// find the corresponding file and 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; // class name to path $ path. = WEBROOT. '/'. $ file; if (file_exists ($ path) {return $ path; // return} return null;} spl_autoload_register ('loader: load ');
The loading rules of classes here are as follows:core__app::run() Corresponding to the root directory/core/app. phprun()Method.spl_autoload_register()Function is automatically loaded. When a class name is called, it is automatically executed.spl_autoload_register('loader::load'), According to the class name include corresponding class file.
The method for executing the app. php entry file starts to run the Framework process.
<? Phpclass 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; // returns the error result;} $ newObj = new $ controller (); call_user_func_array (array ($ newObj, $ method), $ params );}}
Find the corresponding controller based on the request uri, and usecall_user_func_array()Method To Call The method in controller
Root directory/controller/test. php
<?phpclass controller__test { public function write($controller, $method) { //config__test::load('test'); model__test::write($controller, $method); }}
In fact, you do not have to call the test method in the model. You can call any file in the model directory. Before that, you can read some config files and perform other operations.
Root directory/model/test. php
<?phpclass 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 filecore__app::runThe corresponding controller _ test class under the controller will be found and executedwrite()Method