当我将视图放在CI 2.0+中的子文件夹中时,为什么会出现错误?

问题描述:

I am making a simple application and I am familiar with CodeIgniter 1.7.3, which I didn't have any problems putting sub folders into the application/views folder.

My problem is specifically when I try and load the view, I get the standard CI error message of An Error Was Encountered Unable to load the requested file: usercontentpages/testview.php

However if I move the view file out of the sub folder and place it in the application/views folder the load view command works fine?

I can't find any documentation in the user guide on why this is so?

Here is my Controller:

    <?php
class Pages extends CI_Controller
{

//Controller Constructor
    public function __construct()
    {
        parent::__construct();
    }

    //Home Page (Default)
    public function index()
    {       
         //Loading a Partial View
         $mContent = $this->load->view('home', '', TRUE); //this works
        //$mContent = $this->load->view('subfolder/home', '', TRUE); //this doesn't work

         $data = array(
             'mainContent' => $mContent,
         );
         //Passing the partial view to the master page
         $this->load->view('usermasterpage', $data);
    }
}
?>

Here is my Master Page:

<?php
//Doctype
echo doctype('html');
?>
<html>
<head>
    <meta charset="utf-8">

</head>
<body onload="OnLoad()">
    <div id="wrap">

        <!-- Main Content Div -->
        <div id="mainContent">
        <?php 
            /* Every page will have a main content area */
            if(isset($mainContent))
            {
                echo $mainContent;
            }
        ?>
        </div>
    </div>
</body>

And Finally here is the page snippet I'm trying to load:

<p>Hi there</p>

我正在制作一个简单的应用程序,我熟悉CodeIgniter 1.7.3,我没有遇到任何问题 将子文件夹放入application / views文件夹。 p>

我的问题是当我尝试加载视图时,我得到了一个错误被遇到的标准CI错误消息无法加载请求 file:usercontentpages / testview.php p>

但是,如果我将视图文件移出子文件夹并将其放在application / views文件夹中,那么load view命令可以正常工作吗? p>

我在用户指南中找不到为什么会出现这种情况的任何文档? p>

这是我的控制器: p>

 &lt;?php 
class页面扩展CI_Controller 
 {
 
 //控制器构造函数
公共函数__construct()
 {
 parent :: __ construct(); 
} 
 
  //主页(默认)
公共函数索引()
 {
 //加载局部视图
 $ mContent = $ this-&gt; load-&gt; view('home  ','',TRUE);  //这个有用
 // $ mContent = $ this-&gt; load-&gt; view('subfolder / home','',TRUE);  //这不起作用
 
 $ data = array(
'mainContent'=&gt; $ mContent,
); 
 //将局部视图传递给母版页
 $ this-&gt;  load-&gt; view('usermasterpage',$ data); 
} 
} 
?&gt; 
  code>  pre> 
 
 

这是我的母版页: p>

 &lt;?php 
 // Doctype 
echo doctype('html'); 
?&gt; 
&lt; html&gt; 
&lt; head&gt; 
&lt; meta  charset =“utf-8”&gt; 
 
&lt; / head&gt; 
&lt; body onload =“OnLoad()”&gt; 
&lt; div id =“wrap”&gt; 
 
&lt;! -   - 主要内容Div  - &gt; 
&lt; div id =“mainContent”&gt; 
&lt;?php 
 / *每个页面都有一个主内容区域* / 
 if(isset($ mainContent))  
 {
 echo $ mainContent; 
} 
?&gt; 
&lt; / div&gt; 
&lt; / div&gt; 
&lt; / body&gt; 
  code>  pre> 
 \  n 

p>

最后这里是我正在尝试加载的页面片段: p>

 &lt; p&gt; Hi there&lt;  / p&gt; 
  code>  pre> 
  div>

CodeIgniter will successfully use nested views: I use nested views all the time and I know others who do as well, and it works out of the box. Show some code so we can see what you're doing wrong.

The CodeIgniter documentation says precisely that you can.

Your view files can also be stored within sub-folders if you prefer that type of organization. When doing so you will need to include the folder name loading the view. Example:

$this->load->view('folder_name/file_name');

http://codeigniter.com/user_guide/general/views.html