问题
I'm wondering if it is possible to set a custom 404 page on silverstripe specific to a certain page type, while all other 404 errors use the default one.
I have a particular page type "Events" which has event pages unpublished after their specific date.
I also have a static page with links to some of the events. This page url is sent to members so they will still have access to the page even after some of the events may have passed.
Once an event has passed, it is unpublished and therefore following the link will lead to a 404 error page. The link will still be available on the static page.
I am wanting to set a specific error page for the Event page type so that when someone follows the link to a past event they will get error page A. "this event has passed, go to our events page"
I then want a regular error page B "page not found, go to our home page" that will be the error page for any other incorrect links on the site.
Is there any way I can do this?? I am using silverstripe 3
回答1:
Solution for anyone who comes across this:
config.yml
RequestHandler:
extensions:
- ExpiredEventRedirectExtension
ContentController:
extensions:
- ExpiredEventRedirectExtension
ModelAsController:
extensions:
- ExpiredEventRedirectExtension
ExpiredEventRedirectExtension.php
public function onBeforeHTTPError404($request)
{
$urlSegment = $request->param('URLSegment');
if ($urlSegment == "events") {
$response = new SS_HTTPResponse();
$response->redirect("/event-no-longer-available/");
}
throw new SS_HTTPResponse_Exception($response);
}
This redirects the user to event-no-longer-available which is of page type "Page" if the event has been listed below the "events" EventHolder page type. If it hasn't come from this URL a regular 404 error page will show
来源:https://stackoverflow.com/questions/55602288/silverstripe-3-can-i-set-a-custom-error-page-in-silverstripe-for-certain-page-t