Jump to:

23493 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Custom 404 message (ErrorPage)

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

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

Page: 1
Go to End
Author Topic: 1374 Views
  • Xurk
    Avatar
    Community Member
    50 Posts

    Custom 404 message (ErrorPage) Link to this post

    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?

  • BenWu
    Avatar
    Community Member
    90 Posts

    Re: Custom 404 message (ErrorPage) Link to this post

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

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: Custom 404 message (ErrorPage) Link to this post

    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>");

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: Custom 404 message (ErrorPage) Link to this post

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

    1374 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.