PHP implementation of Routing and class automatic loading steps

Source: Internet
Author: User
This time for everyone to bring PHP implementation of the Routing and Class automatic loading steps in detail, PHP implementation of Routing and class automatic loading steps in detail of the considerations are what, the following is the actual case, take a look.

The project directory is as follows

Entry file index.php

<?phpdefine (' WEBROOT ', ' c:/users/administrator/documents/netbeansprojects/test '); require_once (WEBROOT. ') /core/environment.php '); Coreapp::run (); //

class automatically loads files environment.php

<?php//include loader {  ////  ($name) {    $file = self: The file is found according to the class name. FilePath ($name);    if ($file) {      return include $file;    }  }  static function filepath ($name, $ext = '. php ') {    if (! $ext) {      $ext = '. php ';    }    $file = Str_replace (', '/', $name). $ext; The class name to Path    $path. = WEBROOT. '/' . $file;    if (file_exists ($path)) {return      $path;//Find returns    }    return null;}  } Spl_autoload_register (' loader::load ');

The load rules of my class are like the coreapp::run() corresponding root/core/app.php run() method, using the spl_autoload_register() function implementation of automatic loading, when a class name is called, it is automatically executed spl_autoload_register('loader::load') , according to the class name include the corresponding class file.

App.php Portal File Execution method start running frame flow

<?phpclass Coreapp {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;//Show error result return;    } $NEWOBJ = new $controller ();  Call_user_func_array (Array ($NEWOBJ, $method), $params); }}

According to the request URI to find the corresponding controller, call_user_func_array() the way to call the controller in the method

Root directory/controller/test.php

<?phpclass controllertest {public  function write ($controller, $method) {    //configtest::load (' test ');    Modeltest::write ($controller, $method);}  }

In fact, the call does not have to call the model test method, you can adjust the model directory of any file, before this can go to read some config file and so on.

Root directory/model/test.php

<?phpclass modeltest {public  function write ($model, $method) {    echo ' from controller: '. $model. ' to model: '. $model. ', method: '. $method;  }}

For example Hostname/test/write this request will come in from the entry file, and coreapp::run will find the corresponding controller under the Controllertest class, execution write() method

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

Laravel Framework implements search function code parsing

PHP implementation to build a promotional poster steps detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.