【问题标题】:controller for view not working codeigniter视图控制器不起作用codeigniter
【发布时间】:2015-12-06 18:41:28
【问题描述】:

我有两个视图,即views/pages/home.phpviews/pages/search_result.php。我有一个控制器来加载这个视图,即controllers/Pages.php。而且我在视图中还有一个文件夹,即views/templates/header.phpviews/templates/footer.php

当我将浏览器指向 http://localhost/codeigniter/home 时,一切正常。

但问题是当我将浏览器指向http://localhost/codeigniter/search_result 时,视图footer.php 也会显示。其实search_result.php里面什么都没有给

我的控制器代码是,

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Pages extends CI_Controller {

    public function home($page = 'home')
    {
        //code to show home.php (http://localhost/codeigniter/home)
        if (!file_exists(APPPATH.'/views/pages/'.$page.'.php'))
        {
            // Whoops, we don't have a page for that!
            show_404();
        }
       else
       {
            $data['title'] = ucfirst($page); // Capitalize the first letter

            $this->load->view('templates/header', $data);
            $this->load->view('pages/'.$page, $data);
            $this->load->view('templates/footer', $data);
       }
    }

    public function search_result($page = 'search_result') {
        //code to show search_result.php (http://localhost/codeigniter/search_result)
    }

}

search_result 函数内部,我没有给出任何代码,当我指向http://localhost/codeigniter/search_result 时,函数home 的页脚正在显示,即$this-&gt;load-&gt;view('templates/footer', $data);

做错了什么。有什么办法可以解决这个问题。我是codeigniter的初学者。

【问题讨论】:

  • 如果这个地址 localhost/codeigniter/home 调用 Pages 控制器,那么您应该查看 application/config/routes.php 中的一些代码,因为路由是您调用控制器的典型方式名称与 url 地址不同。

标签: php codeigniter


【解决方案1】:

试试这个

public function home()
{
    //code to show home.php (http://localhost/codeigniter/home)
    if (!file_exists(APPPATH.'/views/pages/home.php'))
    {
        // Whoops, we don't have a page for that!
        show_404();
    }
   else
   {
        $this->load->view('templates/header', $data);
        $this->load->view('pages/Home', $data); # changed 
        $this->load->view('templates/footer', $data);
   }
}

【讨论】:

  • 以上代码适用于home.php。但我想显示第二页search_reult.php
猜你喜欢
  • 1970-01-01
  • 2013-01-17
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多