【发布时间】:2018-08-24 11:19:24
【问题描述】:
我想知道如果出现错误,是否可以重定向到自定义错误页面。 所以我有这个例外:
if (empty($user['user'])) {
throw new Exception("Failed to get the correct User", 400);
}
}catch(DAOException $e) {
throw new Exception($e->returnErrorMessage(), $e->returnHttpCode());
}catch(Exception $e) {
throw new Exception($e->getMessage(), $e->getCode());
}
我想将其重定向到自定义错误页面,并显示消息“此激活链接不正确”。 我是否需要更改路线或异常中的某些内容或两者兼而有之? 怎么做? 非常感谢!
【问题讨论】:
-
有很多方法可以做到这一点。你可以
header()到一个不同的位置,你可以只包含一个 php 脚本,许多 MVC 框架会自动将你带到一个错误控制器,所以这取决于你的设置 -
你能告诉我如何在我当前的代码中包含这个吗?
-
嗯,最简单的情况是摆脱异常,并将其替换为
require_once 'my-error-page.php'; exit;。 -
my-error-page.php 会是什么样子?有什么地方可以看吗?我只想在其中显示“激活链接不正确”的消息。
-
你对require_once是什么意思,那是我需要在代码中写的吗?像这样: if (empty($user['user'])) { require_once 'my-error-page.php'; exit;} 那会是什么样子?
标签: php exception error-handling