Silverstripe 3: Can I set a custom error page in silverstripe for certain page types?

雨燕双飞 提交于 2019-12-13 14:17:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!