1

这就是我设置标题$this->output->set_status_header('404'); 的方式 在控制器中:

<?php
class custom404 extends CI_Controller
{
    public function __construct()
    {
            parent::__construct();
    }

    public function index()
    {
        $this->output->set_status_header('404');
        $this->view_data['page_title'] = 'Page not found';
        $this->view_data['main_content'] = 'error404';
        $this->load->view('template', $this->view_data);
    }
}
?>

然后我需要以某种方式检查视图中的状态标题 404?

除了在 $this->view_data 中发送另一个变量之外还有什么建议吗?

4

1 回答 1

3

使用以下代码在 application/core 文件夹中创建一个名为 MY_Output.php 的文件:

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class MY_Output extends CI_Output {

    function set_status_header($code = 200, $text = '')
    {
        set_status_header($code, $text);
        $this->last_set_status_code = $code;
        return $this;
    }

    function get_status_header()
    {
        return $this->last_set_status_code;
    }

}

?>

您现在可以回显您设置的最后一个状态代码,如下所示:

$this->output->set_status_header('404');
echo $this->output->last_set_status_code; // outputs '404'
// or get it like so:
echo $this->output->get_status_header(); // outputs '404'
于 2012-11-29T13:58:11.997 回答