【问题标题】:How to require the file in sub directory in custom PHP MVC?如何在自定义 PHP MVC 中要求子目录中的文件?
【发布时间】:2015-03-25 06:58:26
【问题描述】:

我在 php 中创建了一个 mvc 结构,只是为了在 php 中学习。当我按照在线教程进行操作时,这是一个简单的成功。

当前 mvc 的目录结构: 应用程序 |-控制器 1.php , 2.php , 3.php
|-库 1.php , 2.php , 3.php
|-查看次数 1.php,2.php,3.php |-型号 1.php,2.php,3.php |-公开

控制器的调用由 libs 文件夹中名为 bootstrap.php 的文件完成。

我对目录结构进行了一些更改: 应用程序 |-控制器 |- 模块1 1.php、2.php、3.php

  |- module2
       1.php , 2.php , 3.php 

|-库 |-查看次数 |-型号 |-公开

尝试整合 bootstrap.php 中的更改以调用控制器 从被调用的文件夹中。

这是我的

private $_modulePath = app/controllers/

private function _loadExistingModuleController() {
      $module = $this->_modulePath . $this->_url[0];
        if(is_dir($this->_modulePath . $this->_url[0]) == true) {
        $_dirpath =  $this->_modulePath . $this->_url[0] .'/';
            $file = $this->_dirpath . $this->_url[1];           
                  if (file_exists($file)) {
                require '$file';
                $this->_controller = new $this->_url[1];
                $this->_controller->loadModel($this->_url[1], $this->_modelPath);
            } else {
                $this->_error();
                return false;
            }
        } else {
            $this->_error();
            return false;
        }

    }

我收到未定义的偏移量:

关于如何执行上述操作的任何建议。 谢谢。

【问题讨论】:

  • 取消定义哪一行或哪段代码的偏移量?
  • 现在在 $file = $this->_dirpath 行进行了一些更改。 $this->_url[1];它说未定义的属性。
  • 您能用您目前遇到的问题更新问题吗?我认为消息“未定义的属性”已经足够清楚了。 $this->_dirpath 未定义。

标签: php model-view-controller url-routing bootstrapping


【解决方案1】:

问题:

$this->_dirpath 尚未定义,您正在定义 $_dirpath

解决方案:

要解决您最近的问题更改:

$_dirpath =  $this->_modulePath . $this->_url[0] .'/';

$this->_dirpath =  $this->_modulePath . $this->_url[0] .'/';

【讨论】:

  • 还有其他更好的方法来实现这一点吗?
  • 什么意思?您正在尝试使用未定义的属性!在使用之前定义它是唯一的方法。
猜你喜欢
  • 2019-06-17
  • 2019-06-22
  • 2010-09-12
  • 2019-10-08
  • 2015-11-28
  • 1970-01-01
  • 2016-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多