如何在php路由中使用外部函数

如何在php路由中使用外部函数,php,routes,Php,Routes,我正在构建简单的php路线,但我 我有以下几点,这是有效的 Route::add('/', function(){ echo 'Hello world message'; }); 我的挑战是。。。我希望能够直接从我正在调用的控制器加载函数 use Controllers\LoginController; Route::add('/', LoginController::login()); login()是我在LoginController类中创建的函数 我得到这个错误 <b&

我正在构建简单的php路线,但我 我有以下几点,这是有效的

Route::add('/', function(){
    echo 'Hello world message';
});
我的挑战是。。。我希望能够直接从我正在调用的控制器加载函数

use Controllers\LoginController;

Route::add('/', LoginController::login());
login()是我在LoginController类中创建的函数 我得到这个错误

<b>Warning</b>:  call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in <b>C:\xampp\htdocs\Config\Route.php</b> on line <b>75</b><br />
警告:call\u user\u func\u array()要求参数1为有效回调,在第75行的C:\xampp\htdocs\Config\Route.php中未给出数组或字符串
My Route.php

<?php

namespace Config;

class Route{   private static $routes = Array();   private static $pathNotFound = null;   private static $methodNotAllowed = null;

  public static function add($expression, $function, $method = 'get')  {
    array_push(self::$routes,Array(
      'expression' => $expression,
      'function' => $function,
      'method' => $method
    ));   }

  public static function pathNotFound($function){
    self::$pathNotFound = $function;   }

  public static function methodNotAllowed($function){
    self::$methodNotAllowed = $function;   }

  public static function run($basepath = '/')

  {

    // Parse current url
    $parsed_url = parse_url($_SERVER['REQUEST_URI']);//Parse Uri

    if(isset($parsed_url['path'])){
      $path = $parsed_url['path'];
    }else{
      $path = '/';
    }

    // Get current request method
    $method = $_SERVER['REQUEST_METHOD'];

    $path_match_found = false;

    $route_match_found = false;

    foreach(self::$routes as $route){

      // If the method matches check the path

      // Add basepath to matching string
      if($basepath!=''&&$basepath!='/'){
        $route['expression'] = '('.$basepath.')'.$route['expression'];
      }

      // Add 'find string start' automatically
       //$route['expression'] = '^'.$route['expression'];
 
      //  Add 'find string end' automatically
       $route['expression'] = $route['expression'].'$';

      //echo $path.'<br/>';

      // Check path match 
      if(preg_match('#'.$route['expression'].'#',$path,$matches)){

        $path_match_found = true;

        // Check method match
        if(strtolower($method) == strtolower($route['method'])){

          array_shift($matches);// Always remove first element. This contains the whole string

          if($basepath!=''&&$basepath!='/'){
            array_shift($matches);// Remove basepath
          }
          call_user_func_array($route['function'], $matches);

          $route_match_found = true;

          // Do not check other routes
          break;
        }
      }
    }

    // No matching route was found
    if(!$route_match_found){

      // But a matching path exists
      if($path_match_found){
        header("HTTP/1.0 405 Method Not Allowed");
        if(self::$methodNotAllowed){
          call_user_func_array(self::$methodNotAllowed, Array($path,$method));
        }
      }else{
        header("HTTP/1.0 404 Not Found");
        if(self::$pathNotFound){
          call_user_func_array(self::$pathNotFound, Array($path));
        }
      }

    }

  }

}

它需要回调,因此需要传递函数本身。在本例中,使用括号
()
调用(调用)函数并返回其结果,而不是传递函数本身

下面是一个简单的例子,说明如何做到这一点:

Route::add('/', LoginController::login);

将其包装到另一个函数中,甚至可以传递参数

// shorthand function syntax since 7.4
Route::add('/', fn() => LoginController::login());

// example arguments (just a prototype)
Route::add('/', fn() => LoginController::login($username, $password));


如果使用的是
call\u user\u func\u array()
,则需要确保回调的格式正确。执行此操作时:
Route::add('/',LoginController::login())
,实际上您正在直接执行
LoginController::login()
方法,并将该方法的响应作为路由的回调传递,而不是方法本身。只要将其作为可调用的对象传递,就不需要将其包装在函数中(可以是类似于:
[LoginController::class,'login']
[$objInstance,'login']
等等)@MagnusEriksson我没有说必须换行,请参阅第一行代码。但是如果换行,您可以传递参数,如
fn()=>LoginController::login($username,$password)
。只是补充了这一点作为额外说明。我可能误解了你的意思,但第一句话的措辞听起来像是你在说你需要这样做。:-)只是想为OP和未来的访问者澄清一下。@Magnueriksson更新了回答,以提供更多澄清。
Route::add('/', function() { LoginController::login() });

// example arguments (just a prototype)
Route::add('/', function() { LoginController::login($username, $password) });