Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Custom 404 message (ErrorPage)


Go to End


6 Posts   4701 Views

Avatar
Xurk

Community Member, 50 Posts

25 December 2010 at 3:47am

For a SilverStripe website we're currently working on, we've been making use of the "httpError" method for redirecting users to the 404 page when e.g. a DataObject cannot be found on a certain page. Everything is set up properly; there is an instance of ErrorPage in the Site Tree, I've added an ErrorPage.ss file to the Layout directory of our theme and it is being displayed properly whenever it is supposed to. There's just one thing that has us stumped.

According to the SS Docs, the httpError method can take two arguments:

void httpError( $code, [ $message = null])
We're assuming that $message is for sending a custom message along to the ErrorPage template. But we can't figure out how to display this message and I haven't had any luck searching the forum, the Docs and Google looking for an example.

The reason why we want to do this, is because that way we can use the same template but display a different (sub-)message depending on what the user was trying to access. For example:

return $this->httpError(404, 'Sorry, the portfolio item you are looking for could not be found.');

return $this->httpError(404, 'Sorry, the member you are looking for does not exist.');

Is the reason that I can't seem to display the message in the template because it is only for logging purposes maybe?

Avatar
BenWu

Community Member, 97 Posts

21 July 2012 at 2:40am

mmm. i would also like to know. probably need to put a tag in the Errorpage type??

Avatar
JonoM

Community Member, 130 Posts

29 March 2013 at 10:36am

I also had this problem today - here is the solution I came up with - all it does is presents the current page but with the content and or title overridden and the response code changed. This example uses 404 but you could easily modify the function to accept a different code as an argument.

In Page_Controller

	public function Custom404($content=false,$title=false) {
		if ($content) $this->Content = $content;
		if ($title) $this->Title = $title;
		$response = new SS_HTTPResponse($this->RenderWith('Page'),404);
		$response->output();
		exit; //Maybe not necessary but doesn't hurt
	}

In your code (assuming $this refers to a page)

$this->Custom404("<h2>Error - Page not found</h2><p>Your session may have expired - Please try again</p>");

Avatar
JonoM

Community Member, 130 Posts

29 March 2013 at 10:37am

p.s. this is for 2.4 - don't know if it works for SS3

Avatar
MarijnKampf

Community Member, 176 Posts

30 April 2015 at 10:38pm

I can confirm that this works for SS 3.1

Avatar
mediabeastnz

Community Member, 10 Posts

8 July 2015 at 9:51am

*Bump*

It seems weird that this httpError functions does nothing with the second parameter?

I've created an issue on Github for future reference.
https://github.com/silverstripe/silverstripe-framework/issues/4378