【问题标题】:Including controller in php code in view file在视图文件的 php 代码中包含控制器
【发布时间】:2017-04-14 11:08:26
【问题描述】:

我正在开发一个使用 MVC 架构的 PHP 项目。在我的项目文件中,我有index.php 文件以及model 文件夹、controller 文件夹和view* 文件夹。

在我的索引文件中我写了这段代码:

`<?php
  include("./controller/controllerUser.php");
  $ocont = new controllerUser();
  if (isset($_GET['action']) && !empty($_GET['action'])) {
    $ocont->{$_GET['action']}();
  } 
?>` 

controllerUser(位于 controller 文件夹内)将网站重定向到我的 ViewLogin 文件(位于 view 文件夹内),直到这里一切正常。

问题是当我点击我的viewLogin 文件上的注册按钮时,它会将我重定向到ViewSignup 文件(位于view 文件夹内),但我得到了这些错误:

Warning: include(..\Model\controllerUser.php) [function.include]: failed to    open stream: No such file or directory in    D:\wamp\www\projetphp\view\ViewSignup.php on line 45
Warning: include() [function.include]: Failed opening '.\controller\controllerUser.php' for inclusion (include_path='.;C:\php\pear') in D:\wamp\www\projetphp\view\viewSignup.php on line 45
Fatal error: Class 'controllerUser' not found in D:\wamp\www\projetphp\view\ViewSignIn.php on line 51

我认为问题在于 ViewSignup 页面的 PHP 代码中包含 controllerUser 文件。
这是我的代码:

include  ('.\controller\controllerUser.php');
$ocontuser = new  controllerUser(); 
$ocontuser->Actions2_SignIn();

我也试过了,但出现了更多错误:

include  ('..\controller\controllerUser.php');
$ocontuser = new  controllerUser(); 
$ocontuser->Actions2_SignIn();

我应该怎么做才能修复这个错误?

【问题讨论】:

    标签: php autoload include-path


    【解决方案1】:

    一个解决方法是定义一个应用程序根文件夹。示例

    APP_ROOT = 'c:\www';

    或者在这种情况下:

    APP_ROOT = 'D:\wamp\www\projetphp';

    那么每个文件都包含这样的文件:

    包括 (APP_ROOT . '\controller\controllerUser.php');

    最好创建一个“config.php”并在配置文件中定义“APP_ROOT”。然后每个文件在顶部包含“config.php”,确保在包含 config.php 的每个文件中始终以相同的方式定义 APP_ROOT。如果根更改,更新 APP_ROOT 也很容易。我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 2012-06-29
      • 2015-04-16
      • 1970-01-01
      相关资源
      最近更新 更多