2016-11-30 84 views
2

我正在使用MVC架構在使用PHP項目。在我的項目文件中,我有index.php文件以及model文件夾,controller文件夾和view *文件夾。包含控制器在php代碼中的查看文件

在我的索引文件,我已經寫了這個代碼:

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

controllerUser(位於控制器文件夾內)的網站重定向到我ViewLogin文件(位於視圖內文件夾),直到這裏都工作正常。

問題是,當我點擊註冊按鈕在我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(); 

我應該做些什麼來解決這個問題?

回答

0

一種修復方法是定義應用程序根文件夾。示例

APP_ROOT ='c:\ www';

或在這種情況下:

APP_ROOT = 'd:\瓦帕\ WWW \ projetphp';

然後,每個文件包括這樣的文件:

包括;(APP_ROOT '\控制器\ controllerUser.php')。

在配置文件中創建一個「config.php」並定義「APP_ROOT」是個好主意。然後每個文件在頂部包含「config.php」,確保APP_ROOT總是以包含config.php的每個文件中的相同方式定義。如果根改變,更新APP_ROOT也很容易。我希望這有幫助。