1

I am working on a custom component that displays pages to the user on the front-end. It is possible that a user may try to access a page of the component that no longer exists, and in these instances, I would like to show the 404 page that is already setup. How do you cause Joomla! to display the 404 page from a custom component? Just like with a normal 404 error, I am looking for the address bar not to change. Being able to do this from the model would be preferable.

Thank you for your help.

2

Recent Joomla version can handle the errors based on native PHP exceptions, so just throw an exception with corresponding message and value, Joomla will display the error page set (template, custom…)

if ($someCheck) 
{
  throw new Exception(JText::_('JERROR_LAYOUT_PAGE_NOT_FOUND'), 404);
}

Source: Whats the correct way for exception handling?

4

In your Joomla installation directory reach templates/themeXXX/error.php file.

Edit the error.php file as shown below, locate this line of code: If you don't want to change address bar url then you can use below code

if (($this->error->getCode()) == '404') {
header("HTTP/1.0 404 Not Found");
echo file_get_contents(JURI::root().'alias-of-404-article-page');
exit;
}
  • Thank you raVan96 but unfortunately, this isn't my problem. Your solution would allow me to show a Joomla! article as an error page. While this is helpful, it is not at all what I am trying to do. Instead, I am trying to display the error page (error.php file) as Joomla! would if it encountered a native 404. – Zachary Draper Apr 18 '18 at 21:56
1

In order to call the 404 error page from your component, simply raise a 404 error using JError. For example:

JError::raiseError(404, JText::_('COM_CONTENT_ERROR_ARTICLE_NOT_FOUND'));
  • JError has been deprecated as of Joomla! 2.5. Although it still works, I am looking for a non-deprecated solution. – Zachary Draper May 10 '18 at 1:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.