如何在CodeIgniter中检查set_status_header是否设置为404?

时间:2012-11-29 13:37:35

标签: php codeigniter

这是我在控制器中设置标题 $ 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-&gt; view_data?

中发送另一个变量之外,还有什么建议

1 个答案:

答案 0 :(得分:2)

使用以下代码在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'