自动加载 + 命名空间在本地主机上工作,但在服务器上不起作用
Autoload + Namespace works in localhost, but don't work in server
我有一个在本地主机上有效但在我的服务器上不起作用的代码,我有一个名为 platform 的文件夹,路径是 /var/www/html/platform
平台/.htaccess
AcceptPathInfo On
RewriteEngine on
RewriteBase /var/www/html/platform/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request= [L,QSA]
平台/autoload.php
function __autoload($className) {
$file = $className . '.php';
if(file_exists($file)) {
require_once $file;
}else{
//fail
}
平台/index.php
include ('autoload.php');
$controller = new application\controllers\Controller();
plataform/application/controllers/Controller.php
namespace application\controllers;
class Controller{
}
在我的本地主机中,此代码有效,但在我的服务器中,我收到以下消息:
Fatal error: Class 'application\controllers\controller' not found in
/var/www/html/platform/index.php on line 12
我该如何解决这个问题?我正在使用 Ubuntu PHPMyAdmin on 14.04 (Digital Ocean)
.
注册自动装带器。
*
* 基于此处找到的官方 PSR-4 自动加载器示例:
* https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
这是一个简单的自动加载 class,也应该处理命名空间
class Autoload {
public function __construct(){
spl_autoload_register(function ($class) {
// project-specific namespace prefix
$prefix = 'App\';
// For backwards compatibility
$customBaseDir = '';
// base directory for the namespace prefix
$baseDir = $customBaseDir ?: __DIR__ . '/';
// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}
// get the relative class name
$relativeClass = substr($class, $len);
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = rtrim($baseDir, '/') . '/' . str_replace('\', '/', $relativeClass) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
});
}
}
保存在 Autoload.php class 中。包含路径并初始化使用.. $autoload = new Autoload;
我有一个在本地主机上有效但在我的服务器上不起作用的代码,我有一个名为 platform 的文件夹,路径是 /var/www/html/platform
平台/.htaccess
AcceptPathInfo On
RewriteEngine on
RewriteBase /var/www/html/platform/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request= [L,QSA]
平台/autoload.php
function __autoload($className) {
$file = $className . '.php';
if(file_exists($file)) {
require_once $file;
}else{
//fail
}
平台/index.php
include ('autoload.php');
$controller = new application\controllers\Controller();
plataform/application/controllers/Controller.php
namespace application\controllers;
class Controller{
}
在我的本地主机中,此代码有效,但在我的服务器中,我收到以下消息:
Fatal error: Class 'application\controllers\controller' not found in /var/www/html/platform/index.php on line 12
我该如何解决这个问题?我正在使用 Ubuntu PHPMyAdmin on 14.04 (Digital Ocean)
.
注册自动装带器。 * * 基于此处找到的官方 PSR-4 自动加载器示例: * https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
这是一个简单的自动加载 class,也应该处理命名空间
class Autoload {
public function __construct(){
spl_autoload_register(function ($class) {
// project-specific namespace prefix
$prefix = 'App\';
// For backwards compatibility
$customBaseDir = '';
// base directory for the namespace prefix
$baseDir = $customBaseDir ?: __DIR__ . '/';
// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}
// get the relative class name
$relativeClass = substr($class, $len);
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = rtrim($baseDir, '/') . '/' . str_replace('\', '/', $relativeClass) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
});
}
}
保存在 Autoload.php class 中。包含路径并初始化使用.. $autoload = new Autoload;