spl autoload ignore class 不存在
spl autoload ignore class that don't exist
我在 php 中有一个简单的 MVC 应用程序,它将第一个查询字符串映射到控制器名称,第二个映射到操作,再映射为参数。自动加载 类 时,它使用正则表达式查找命名约定,这很好,但它不会加载显然存在的 类。
spl_autoload_register(function ($class) {
if (preg_match('/[a-zA-Z]+Controller$/', $classname)) {
if (class_exists(__DIR__ . '/controllers/' . $classname)) {
//never gets to here, even though the file gets
//included by the require statement below
}
require __DIR__ . '/controllers/' . $classname . '.php';
return true;
}
});
//controller and action are "default" and "index" by default
//If a query string is passed, it gets the parts
$controller = $url[0];
$action = $url[1];
$controller_name = ucfirst($controller) . "Controller";
$action_name = $action . "Action";
if (class_exists($controller_name)) {
$controller_object = new $controller_name($request, $config);
$controller_object->$action_name();
} else {
echo "Class doesn't exist : $controller_name";
}
示例输出:
url.com/ = Class doesn't exist "DefaultController"
url.com/default = Class doesn't exist "DefaultController"
url.com/test = Class doesn't exist "TestController"
DefaultController 存在于控制器目录中。
试试这个:
try {
$controller_object = new $controller_name($request, $config);
$controller_object->$action_name();
} catch (Exception $e) {
echo "Class doesn't exist : $controller_name";
}
您的条件调用了 class_exists()
函数,您打算检查文件:
spl_autoload_register(function ($class) {
if (preg_match('/[a-zA-Z]+Controller$/', $classname)) {
if (file_exists(__DIR__ . '/controllers/' . $classname)) {
require __DIR__ . '/controllers/' . $classname . '.php';
return true;
}
}
});
我在 php 中有一个简单的 MVC 应用程序,它将第一个查询字符串映射到控制器名称,第二个映射到操作,再映射为参数。自动加载 类 时,它使用正则表达式查找命名约定,这很好,但它不会加载显然存在的 类。
spl_autoload_register(function ($class) {
if (preg_match('/[a-zA-Z]+Controller$/', $classname)) {
if (class_exists(__DIR__ . '/controllers/' . $classname)) {
//never gets to here, even though the file gets
//included by the require statement below
}
require __DIR__ . '/controllers/' . $classname . '.php';
return true;
}
});
//controller and action are "default" and "index" by default
//If a query string is passed, it gets the parts
$controller = $url[0];
$action = $url[1];
$controller_name = ucfirst($controller) . "Controller";
$action_name = $action . "Action";
if (class_exists($controller_name)) {
$controller_object = new $controller_name($request, $config);
$controller_object->$action_name();
} else {
echo "Class doesn't exist : $controller_name";
}
示例输出:
url.com/ = Class doesn't exist "DefaultController"
url.com/default = Class doesn't exist "DefaultController"
url.com/test = Class doesn't exist "TestController"
DefaultController 存在于控制器目录中。
试试这个:
try {
$controller_object = new $controller_name($request, $config);
$controller_object->$action_name();
} catch (Exception $e) {
echo "Class doesn't exist : $controller_name";
}
您的条件调用了 class_exists()
函数,您打算检查文件:
spl_autoload_register(function ($class) {
if (preg_match('/[a-zA-Z]+Controller$/', $classname)) {
if (file_exists(__DIR__ . '/controllers/' . $classname)) {
require __DIR__ . '/controllers/' . $classname . '.php';
return true;
}
}
});