【问题标题】:Wrong files' paths in php projectphp项目中错误的文件路径
【发布时间】:2016-04-28 23:48:14
【问题描述】:

这是我的 php 项目的结构

文件 index.phpconfig.php 位于根文件夹中。

config.php 文件中我定义了 libs 文件夹的路径

<?php 

DEFINE('LIBS', 'app/Libs/');

这是 index.php 文件夹

<?php 

require 'config.php';

function __autoload($class) {
        require LIBS. $class. ".php";
    }

$bootstrap = new Bootstrap();
$bootstrap->init();

//Composer autoloader
require 'vendor/autoload.php';

这是Bootstrap.php文件

<?php 

class Bootstrap {
private $_url = null;
private $_controller = null;

private $_controllerPath = '/../controllers/';
private $_modelPath = 'models/';
private $_errorFile = 'error.php';
private $_defaultFile = 'home.php';

public function init(){
    $this->_parseUrl();

    if(empty($this->_url[0])){
        $this->_loadDefaultController();
        return false;
    }   

    $this->_loadExistingController();
    $this->_callControllerMethod(); 
}

private function _parseUrl(){
    $url = isset($_GET['url']) ? $_GET['url'] : null;
    $url = rtrim($url, '/');
    $url = filter_var($url, FILTER_SANITIZE_URL);
    $this->_url = explode('/', $url);
}

private function _loadDefaultController() {
    require $this->_controllerPath . $this->_defaultFile;
    $this->_controller = new Home();
    $this->_controller->index();    
}

private function _loadExistingController() {
    $file = $this->_controllerPath . $this->_url[0] . '.php';
    if(file_exists($file)) {
        require $file;
        $this->_controller = new $this->_url[0];
        $this->_controller->loadModel($this->_url[0], $this->_modelPath);   
    } else {
        $this->_error();
        return false;
    }           
}

private function _callControllerMethod() {
    if(isset($this->_url[2])) {
        if(method_exists($this->_controller, $this->_url[1])) {
            $this->_controller->{$this->_url[1]}($this->_url[2]);
        } else {
            $this->_error();
        }
    } else {
        if(isset($this->_url[1])) {
            if(method_exists($this->_controller, $this->_url[1])) {
                $this->_controller->{$this->_url[1]}();
            } else {
                $this->_error();
            }
        } else {
            $this->_controller->index();
        }
    }
}

private function _error() {
    require $this->_controllerPath . $this->_errorFile;
    $this->_controller = new Error();
    $this->_controller->index();
    exit;
}

}

问题是,Bootstrap 类以正确的方式检测 Home 控制器,但它没有找到其他控制器,例如用户控制器,并尝试使用错误控制器,但没有找到错误索引视图。

Warning: require(/../views/error/index.php): failed to open stream: No such file or directory in C:\xampp\htdocs\project\app\libs\View.php on line 9

Fatal error: require(): Failed opening required '/../views/error/index.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\project\app\libs\View.php on line 9

我猜路径有问题,但我不知道如何修复它。如何让 Bootstrap 找到其他控制器?也许我应该改变项目结构?这是我第一次尝试创造一些东西,所以我只是想弄清楚。

【问题讨论】:

  • 你的意思是`$_controllerPath = '../controllers/';`?
  • 不,在这种情况下,bootstrap 没有找到任何控制器,包括 Home 控制器。警告:需要(../controllers/home.php):无法打开流:第 48 行的 C:\xampp\htdocs\project\app\libs\Bootstrap.php 中没有这样的文件或目录致命错误:需要() : 在第 48 行的 C:\xampp\htdocs\project\app\libs\Bootstrap.php 中打开所需的 '../controllers/home.php' (include_path='.;C:\xampp\php\PEAR') 失败
  • 在 Unix 上,文件名不区分大小写,但在 Windows 中是。将home.php 更改为Home.php
  • 错误是一样的 警告:require(../controllers/Home.php): failed to open stream: No such file or directory in C:\xampp\htdocs\project\app\libs\第 48 行的 Bootstrap.php 致命错误:require():在 C:\xampp\htdocs\ 中打开所需的 '../controllers/Home.php' (include_path='.;C:\xampp\php\PEAR') 失败project\app\libs\Bootstrap.php 在第 48 行
  • @Federico 在 *nix 中正好相反,文件名区分大小写,而 windows 则不区分。

标签: php


【解决方案1】:

由于您正在创建一个新项目并且已经使用 Composer,因此您最好遵循 PSR-4 的自动加载规范。您无需实现自动加载器。使用 composer,您甚至可以使用 classmap 为不遵循 PSR-0/4 的项目定义自动加载。但是对于旧的遗留代码库,类映射自动加载只是存在,如果您开始一个新项目,您应该遵循 PSR-0/4。


就是说,您可以对当前代码进行哪些更改:

改变

DEFINE('LIBS', '/app/Libs/');

define('LIBS', __DIR__ . '/app/Libs/');

__DIR__是一个magic constant,在这种情况下代表config.php的目录的绝对路径。我的建议是永远不要依赖相对路径。同理改变

require $this->_controllerPath . $this->_defaultFile;
// and
require $this->_controllerPath . $this->_errorFile;
// and
$file = $this->_controllerPath . $this->_url[0] . '.php';

require __DIR__ . $this->_controllerPath . $this->_defaultFile;
// and
require __DIR__ . $this->_controllerPath . $this->_errorFile;
// and 
$file = __DIR__ . $this->_controllerPath . $this->_url[0] . '.php';

【讨论】:

  • 我使用了你的解决方案define('LIBS', __DIR__ . '/app/Libs/');,它适用于控制器,但是我在其他页面上遇到了一个新的 css 文件问题(在主页上没问题)。我在header.php 文件&lt;link rel="stylesheet" href="public/css/style.min.css"&gt; &lt;link rel="stylesheet" href="public/css/font-awesome.min.css"&gt; 中添加了css 文件,但是在用户索引页面上有错误i.imgur.com/i61syDQ.png 似乎项目试图像控制器i.imgur.com/nCkIwZ4.png 一样包含它们
  • 分享你的.htaccess文件
  • RewriteEngine On RewriteBase /project/ RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.+)$ index.php?url=$1 [QSA,L] Options -Indexes
  • 可以加Options -MultiViews吗?如果不行,也试试Options +FollowSymLinks
  • 我刚刚尝试过,但 css 文件的问题相同。
猜你喜欢
  • 2016-01-08
  • 2012-09-02
  • 2020-11-12
  • 1970-01-01
  • 2014-09-10
  • 2011-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多