CodeIgniter添加页面不起作用

时间:2012-09-03 21:11:05

标签: php codeigniter

我接管了一个用CodeIgniter编写的项目,这是我以前从未使用过的。我在一个名为features.php的视图/页面中添加了一个新文件,并在互联网上阅读了要使其可访问的内容,我需要在控制器文件中创建一个将呈现页面的函数。

我尝试了以下内容:

public function features()
{
    $this->render('template', 'pages/features');
}

然而,当我尝试打开features.php时,它给了我404.我该如何解决?

更新1 - 类

这是控制器的类代码:

class Pages extends MY_Controller {

function __construct()
{
    parent::__construct();
    $this->load->model('setting_model', 'setting');
    $this->load->model('order_model', 'order');
    $this->load->model('page_model', 'page');
    $this->load->library('form_validation');
    $this->load->helper(array('inflector', 'string'));
}

public function index()
{
    $settings = $this->setting->get_settings();
    $data['document_price'] = $settings->document_price;
    $this->render('template', 'pages/index', $data); 
}

    //This works fine
public function about_us()
{
    $this->render('template', 'pages/about_us');
}

    //Here is the problem, although it follows the same pattern as about_us()
public function features()
{
    $this->render('template', 'pages/features');
}



}

2 个答案:

答案 0 :(得分:2)

当你使用$ this->渲染时我猜你正在使用模板库。我认为你应该使用:

public function features()
{
   $this->template->set_template('template');
   $this->template->write_view('pages/features');    
   $this->template->render();
}

答案 1 :(得分:2)

通过输入某些URL无法直接访问/views中包含的php文件。 CodeIgniter是一个MVC框架。这意味着您的URL映射到您的控制器,控制器调用视图。

此函数封装的类的名称是什么?请发布整个课程而不仅仅是features()功能,我们可以帮助您。如果您在本地工作,则呼叫控制器的默认映射为:http://localhost/appname/controller/function/param1/param2/etc

$this->render()函数不是vanilla CodeIgniter语法,您要么继承了使用模板库的项目,要么在控制器类中有一个兄弟render()函数。

检查您的config/routes.php文件并考虑发布。

如果您想诊断问题,请尝试删除对$this->render()的调用,而不是使用CodeIgniter的本机$this->load->view('pages/features')功能进行精确定位。如果这样可行,我们可以确定它是库或render()调用。