DaveWebSmith
DaveWebSmith

Reputation: 1

CI Router no result 404 error

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?

Upvotes: 0

Views: 66

Answers (1)

jleft
jleft

Reputation: 3457

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

Upvotes: 2

Related Questions