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.

3 Answers 3

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;
}
1
  • 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. Apr 18, 2018 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'));
1
  • JError has been deprecated as of Joomla! 2.5. Although it still works, I am looking for a non-deprecated solution. May 10, 2018 at 1:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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