Forum

Thread tagged as: Question, Runway

Trigger a 404?

Is there a way to trigger a 404 error in Perch Runway - say I'm showing the detail page for a collection item (as defined by its template route). If perch_collection() with its options doesn't return any results, I'd like to be able to show my 404 error page (as well as output the correct HTTP code. Is this possible?

Paul Bell

Paul Bell 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, you should be able to call:

http_response_code(404);

Thanks - that does set the correct response code I believe. I was wondering if it was possible to tell Perch that this should be a 404 page - so Perch shows the nice error page I made (the one assigned to be an error page in Perch Runway).

Drew McLellan

Drew McLellan 2638 points
Perch Support

Hmm, ok. So if a route matches and points to a valid page, but that page determines that it doesn't have a matching item?

Yes, that's what I'm after.

On a related matter, is there a way to detect if perch_collection() returns any results or not (without returning the result and checking it in some way?)

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can use a noresults section, or if you're using skip-template you can count the array.

OK, that's helpful. But I guess there's no way to get Runway to show its 404 error in certain circumstances.

I guess one approach that could work would be to make a layout that contains the error page, then if you do it at the top of the page you could, under certain circumstances - output change the http code - output the error layout - exit();

Would that work?

Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't think you'd need the layout. You can just include the error page.

Brilliant - thanks. I could give that a go.

Drew McLellan said:

Yes, you should be able to call:

http_response_code(404);

Hi Drew, Having some problems setting up my error pages. Is there documentation on building error pages for Runway?

Where would you put the response code shown above?

Thanks, Tim