自动加载+命名空间在本地主机上工作,但在服务器上不起作用


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=$1 [L,QSA]

plataform/ autoload.php

function __autoload($className) {
$file = $className . '.php';
if(file_exists($file)) {
     require_once $file;
}else{
    //fail
}

plataform/ index . php

include ('autoload.php');
$controller = new application'controllers'Controller();

plataform/应用程序/控制器/ Controller.php

namespace application'controllers;
class Controller{
}

在我的本地主机这个代码工作,但在我的服务器我收到以下消息:

致命错误:类'application'controllers'controller'不在/var/www/html/platform/index.php第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 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类中。包括path并初始化为使用..$ autolload = new autolload;

相关文章: