Forum

Thread tagged as: Question, Runway

Display 404 Page in Runway if route matches but no content

I'm looking to force show my custom 404 page in Runway if route matches but there is no content for a collection item. Currently, we just get a templated page with no content.

Using PerchSystem::use_error_page(404); has been mentioned before but there doesn't appear to be anything documented for use with Collections.

How do I perform the check correctly and redirect to the 404 if there is no post? This is what I have so far - but it doesn't redirect to my 404 when testing - it embeds the 404 template in my post template.

  # get the post
  $post = perch_collection('News', [
        'filter'        => 'titleSlug',
        'match'         => 'eq',
        'value'         => perch_get('s'),
        'skip-template' => 'true',
        'return-html'   => 'true',
      ]);

if (empty(array_filter($post))) {
  PerchSystem::use_error_page(404);
} else {
  echo $post['html'];
}
Dan Lee

Dan Lee 1 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Make sure you call PerchSystem::use_error_page before outputting anything else.

Dan Lee

Dan Lee 1 points

Ah super - that makes sense - so wrap the template in the if statement.