doumei4964 2014-05-26 19:49
浏览 67

CI路由器没有结果404错误

I'm working on a project to get a vanity_url to resolve a record from the database.

e.g. abc.com/ThisRecord -> shows the record from database where vanity_url=ThisRecord

In the route file I have used this:

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

How would I get an error 404 if no such record exists?

  • 写回答

1条回答 默认 最新

  • douren7921 2014-05-26 20:35
    关注

    CodeIgniter has a show_404() function which will send an HTTP 404 header and return the template found in: application/errors/error_404.php. See the user guide for more information.

    When calling the list_page() function, you just need to check if the record exists. If the record does exist, load the relevant view, otherwise, call the show_404() function.

    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();
        }
    }
    
    评论

报告相同问题?