Implementation code of PHP simple routing and class automatic loading function

小云云
Release: 2023-03-21 10:48:01
Original
1396 people have browsed it

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(&#39;WEBROOT&#39;, &#39;C:/Users/Administrator/Documents/NetBeansProjects/test&#39;);
require_once(WEBROOT.&#39;/core/environment.php&#39;);
core__app::run(); //
Copy after login

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 = &#39;.php&#39;) {
    if (!$ext) {
      $ext = &#39;.php&#39;;
    }
    $file = str_replace(&#39;__&#39;, &#39;/&#39;, $name) . $ext; //类名转路径
    $path .= WEBROOT . &#39;/&#39; . $file;
    if (file_exists($path)) {
      return $path; //找到就返回
    }
    return null;
  }
}
spl_autoload_register(&#39;loader::load&#39;);
Copy after login

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.

App.php entry file execution method starts running the framework process


<?php
class core__app {
  static function run() {
    $a = $_SERVER[&#39;REQUEST_URI&#39;];
    $uri = rtrim(preg_replace(&#39;/\?.*/&#39;, &#39;&#39;, $_SERVER[&#39;REQUEST_URI&#39;]), &#39;/&#39;);
    $params = explode(&#39;/&#39;, trim($uri, &#39;/&#39;));
    $count = count($params);
    if ($count > 1) {
      $controller = $params[0];
      $method = $params[1];
    } elseif ($count == 1) {
      $controller = &#39;index&#39;;
      $method = $params[0];
    } else {
    }
    $filename = WEBROOT . &#39;/controller/&#39; . $controller . &#39;.php&#39;;
    $controller = &#39;controller__&#39;.$controller;
    try {
      if (!file_exists($filename)) {
        throw new Exception(&#39;controller &#39; . $controller . &#39; is not exists!&#39;);
        return;
      }
      include($filename);
      if (!class_exists($controller)) {
        throw new Exception(&#39;class &#39; . $controller . &#39; is not exists&#39;);
        return;
      }
      $obj = new ReflectionClass($controller);
      if (!$obj->hasMethod($method)) {
        throw new Exception(&#39;method &#39; . $method . &#39; is not exists&#39;);
        return;
      }
    } catch (Exception $e) {
      echo $e; //展示错误结果
      return;
    }
    $newObj = new $controller();
    call_user_func_array(array($newObj, $method), $params);
  }
}
Copy after login

Find the corresponding controller according to the request uri, use

call_user_func_array() method to call the method in the controller

Root directory/controller/test.php


<?php
class controller__test {
  public function write($controller, $method) {
    //config__test::load(&#39;test&#39;);
    model__test::write($controller, $method);
  }
}
Copy after login

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 &#39;From controller:&#39;.$model.&#39; to model: &#39; . $model . &#39; ,method: &#39; . $method;
  }
}
Copy after login

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

Related recommendations:

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
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!