CI路由器没有结果404错误

时间:2014-05-26 19:49:23

标签: php codeigniter url routes

我正在开发一个项目来获取vanity_url来解析数据库中的记录。

e.g。 abc.com/ThisRecord - >显示数据库中的记录,其中vanity_url = ThisRecord

在我使用过的路径文件中:

$route['(:any)'] = 'listing_controller/list_page/$1'; // Resolved the Vanity_URL Query

如果不存在此类记录,我将如何获得错误404?

1 个答案:

答案 0 :(得分:2)

CodeIgniter有一个show_404()函数,它将发送HTTP 404标头并返回在application/errors/error_404.php中找到的模板。有关详细信息,请参阅user guide

调用list_page()函数时,只需要检查记录是否存在。如果记录确实存在,请加载相关视图,否则,请调用show_404()函数。

function list_page($id)
{
    if (/* the record exists */)
    {
        // The record does exist - do what you want, load a view etc.
        $this->load->view('your_view');
    }
    else
    {
        // The record doesn't exist, show the 404 page
        show_404();
    }
}