CodeIgniter不按预期加载视图

时间:2015-06-16 01:14:08

标签: codeigniter view

我部署了一个应用程序,但我遇到了问题。这是我的控制者:

class Account extends CI_Controller {
  function test() {
    echo "hello";
    $this->load->view('account/test');
    echo "hello again";
  }

输出显示hellohelloagain

未调用load->view()方法。这发生在所有控制器中。 在开发环境中不存在这个问题。

我已经:

  • 删除了挂钩
  • 已检查的权限

我的Apache日志中没有警告或错误。

更新

我发现了一些奇怪的东西:

function index() {
    $this->load->view('welcome/index'); // NOTHING

    $output = $this->load->view('welcome/index', array(), true);
    echo $output; // WORKS FINE
}

3 个答案:

答案 0 :(得分:1)

class Account extends CI_Controller 
{
  function test() {
    echo "hello";// No 01
    $this->load->view('account/test'); //No 02
    echo "hello again"; //No 03
 }

No 01 中,它会显示您提到的hello字。再次在 No 02 中加载视图(此视图为空白页,否则您将看不到错误页面)。

错误是:

  

遇到错误

     

无法加载请求的文件:account / test.php

因此,您的测试视图是空白页面。然后在 No 03 你正在通过hello again。所以这也会出现在你的视图中。

所以你的最终输出为hellohello again

非常好意味着您的代码运行良好。

测试您的视图是否正常工作

在您的视图(test.php)中写下此行并查看输出

<h1>Hi, I'm here</h1>

阅读本文以获取进一步的知识

  1. Views
  2. Controllers
  3. Models

答案 1 :(得分:1)

您是否尝试为每个回声创建单独的视图?

    class Account extends CI_Controller {
        function test() {
            $this->load->view('account/hello');
            $this->load->view('account/test');
            $this->load->view('account/hello-again');
        }
    }

答案 2 :(得分:0)

解决:

我在钩子配置中犯了一个错误:

 $hook['display_override'][] = array('class' => 'layout', 'function' => 'show_layout', 'filename' => 'layout.php', 'filepath' => 'hooks');

实际上,真实姓名是Layout.php,而配置需要layout.php。

谢谢大家