Lemonstand product & category throw 404 if not found

January 02, 2014

Lemonstand weirdly does not come with a way to configure it’s dynamic pages such as categories and products to throw automatically throw a 404 if the product was not found.

I solved this with the following simple snippet. Initially just added to the post_action.php for both the category/ and product/ pages.

product/post_action.php

if(!isset($product)){
  $params = array();

  $page = Cms_Page::findByUrl('/404', $params);
  header('HTTP/1.0 404 Not Found');
  Cms_Controller::get_instance()->open($page,$params);
  exit;
}

category/post_action.php

if(!isset($category)){
  $params = array();

  $page = Cms_Page::findByUrl('/404', $params);
  header('HTTP/1.0 404 Not Found');
  Cms_Controller::get_instance()->open($page,$params);
  exit;
}

An improvement to this could be to wrap the 404 throwing into a function provided by a custom module and then just keep the logic behind throwing the 404 in the relevant areas of the site. In this case I just couldn’t be bothered…

About me

Hello! I'm David Heward, how are you going? I'm a Senior Devops/Build Engineer, specialising in AWS & Cloud Automation. Based in London. Strong 10+ year background in Software development. Have a read of my blog. Have a look at some of my working projects. Contact me at @davehewy or on Linkedin.