简体   繁体   English

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

[英]How to check if set_status_header is set to 404 in view in CodeIgniter?

This is how I set header $this->output->set_status_header('404'); 这就是我设置header $ this-> output-> set_status_header('404')的方法; in controller: 在控制器中:

<?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);
    }
}
?>

Then I need to somehow check status header 404 in the view? 然后我需要以某种方式检查视图中的状态标题404?

Any advice beside sending another variable in $this->view_data? 除了在$ this-> view_data中发送另一个变量之外的任何建议?

Create a file called MY_Output.php in your application/core folder with the following code: 使用以下代码在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;
    }

}

?>

You can now echo out the last status code you set like so: 您现在可以回显出您设置的最后一个状态代码,如下所示:

$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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM