jonom/silverstripe-custom-errors

SilverStripe CMS 的主题自定义错误响应

资助包维护!
jonom
www.paypal.me/jmnz

安装: 589

依赖: 0

建议者: 0

安全性: 0

星标: 3

关注者: 4

分支: 0

开放问题: 1

类型:silverstripe-vendormodule

1.0.5 2018-08-21 19:24 UTC

This package is auto-updated.

Last update: 2024-08-28 04:05:05 UTC


README

概览

作为错误页面模块的替代方案,此模块提供主题化错误响应,但让开发者负责错误消息的内容,而不是 CMS 用户。

安装

$ composer require jonom/silverstripe-custom-errors

配置

您可以通过 yml 配置 API 为每个响应代码定义默认响应内容。响应代码需要以前缀字母 'e' 开头,因为仅数字不是有效的 SS 配置键。您指定的字段将传递到页面模板进行渲染。您可以仅指定一个值,或者通过在数组中指定详细信息来设置一个值。示例

---
Name: my-custom-errors
After:
    - '#custom-errors'
---
JonoM\CustomErrors\CustomErrorControllerExtension:
  custom_fields:
    default:
      Content:
        Type: 'SilverStripe\ORM\FieldType\DBHTMLText'
        Value: '<p>Sorry, there was a problem with handling your request.</p>'
    e404:
      Title: 'Not Found'
      Content:
        Type: 'SilverStripe\ORM\FieldType\DBHTMLText'
        Value: '<p>Sorry, it seems you were trying to access a page that doesnʼt exist.</p><p>Please check the spelling of the URL you were trying to access and try again.</p>'

您还可以指定用于错误响应的默认控制器和模板。

JonoM\CustomErrors\CustomErrorControllerExtension:
  default_controller: 'PageController'
  default_template: 'Page' # exclude .ss extension

自定义错误响应

您可以从控制器中调用 $this->httpError($statusCode, $errorMessage) 并获取一个主题化响应,但如果已为给定的状态代码提供了默认内容,则您的 $errorMessage 不会显示。这是为了确保您可以控制用户可能看到的全部错误消息,而不仅仅是触发在您自己的代码中的错误消息。

要从控制器返回自定义错误响应,您可以使用 $this->customError() 而不是调用 $this->httpError(),并以与使用 renderWith() 相同的方式传递自定义字段。示例

if ($somethingWentWrong) {
    $this->customError(404, [
        'Title' => 'We couldn\'t find the droids you are looking for',
        'Content' => DBField::create_field('HTMLText', '<p>Please try another Cantina</p>')
    ]);
}

您指定的任何字段都会与默认值合并,因此您只需指定要覆盖的字段。

$this->customError(404, [
    'Content' => DBField::create_field('HTMLText', '<p>Maybe try the Google</p>')
]);

您还可以指定要使用的控制器和模板。

$this->customError(
    404,
    [
        'Content' => DBField::create_field('HTMLText', '<p>Here are some other droids you may be interested in:</p>'),
        'Droids' => Droid::get()->filterAny(['Language' => 'Beeps', 'Color' => 'Gold'])->limit(5)
    ],
    'DroidHolder',
    'DroidHolderController'
);

维护者联系方式

Jono Menz

赞助

如果您想提高维护者的士气,您可以通过 GitHub 进行小额月捐,或者通过 PayPal 进行一次性捐赠。❤️ 感谢!

如果您想雇佣维护者开发新功能或讨论其他机会,也请随时联系