如何在 php 路由中使用外部函数
How to use external function in php route
我正在构建简单的 php 路线但是我
我有以下内容,可以正常工作
Route::add('/', function(){
echo 'Hello world message';
});
我的挑战是...我希望能够直接从我正在调用的控制器加载函数
use Controllers\LoginController;
Route::add('/', LoginController::login());
login() 是我在 LoginController 中创建的函数 class
我得到这个错误
<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 />
我的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));
}
}
}
}
}
我可以在路由中使用外部函数吗?
它想要一个回调,所以你需要传递一个函数本身。在您的情况下,使用括号 ()
调用(调用)函数和 returns 它的结果而不是传递函数本身。
所以这里有一个简单的例子来说明如何做到这一点:
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));
或
Route::add('/', function() { LoginController::login() });
// example arguments (just a prototype)
Route::add('/', function() { LoginController::login($username, $password) });
我正在构建简单的 php 路线但是我 我有以下内容,可以正常工作
Route::add('/', function(){
echo 'Hello world message';
});
我的挑战是...我希望能够直接从我正在调用的控制器加载函数
use Controllers\LoginController;
Route::add('/', LoginController::login());
login() 是我在 LoginController 中创建的函数 class 我得到这个错误
<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 />
我的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));
}
}
}
}
}
我可以在路由中使用外部函数吗?
它想要一个回调,所以你需要传递一个函数本身。在您的情况下,使用括号 ()
调用(调用)函数和 returns 它的结果而不是传递函数本身。
所以这里有一个简单的例子来说明如何做到这一点:
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));
或
Route::add('/', function() { LoginController::login() });
// example arguments (just a prototype)
Route::add('/', function() { LoginController::login($username, $password) });