Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

113 rindas
3.3KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\web;
  8. use Yii;
  9. use yii\base\Action;
  10. use yii\base\Exception;
  11. use yii\base\UserException;
  12. /**
  13. * ErrorAction displays application errors using a specified view.
  14. *
  15. * To use ErrorAction, you need to do the following steps:
  16. *
  17. * First, declare an action of ErrorAction type in the `actions()` method of your `SiteController`
  18. * class (or whatever controller you prefer), like the following:
  19. *
  20. * ```php
  21. * public function actions()
  22. * {
  23. * return [
  24. * 'error' => ['class' => 'yii\web\ErrorAction'],
  25. * ];
  26. * }
  27. * ```
  28. *
  29. * Then, create a view file for this action. If the route of your error action is `site/error`, then
  30. * the view file should be `views/site/error.php`. In this view file, the following variables are available:
  31. *
  32. * - `$name`: the error name
  33. * - `$message`: the error message
  34. * - `$exception`: the exception being handled
  35. *
  36. * Finally, configure the "errorHandler" application component as follows,
  37. *
  38. * ```php
  39. * 'errorHandler' => [
  40. * 'errorAction' => 'site/error',
  41. * ]
  42. * ```
  43. *
  44. * @author Qiang Xue <qiang.xue@gmail.com>
  45. * @since 2.0
  46. */
  47. class ErrorAction extends Action
  48. {
  49. /**
  50. * @var string the view file to be rendered. If not set, it will take the value of [[id]].
  51. * That means, if you name the action as "error" in "SiteController", then the view name
  52. * would be "error", and the corresponding view file would be "views/site/error.php".
  53. */
  54. public $view;
  55. /**
  56. * @var string the name of the error when the exception name cannot be determined.
  57. * Defaults to "Error".
  58. */
  59. public $defaultName;
  60. /**
  61. * @var string the message to be displayed when the exception message contains sensitive information.
  62. * Defaults to "An internal server error occurred.".
  63. */
  64. public $defaultMessage;
  65. /**
  66. * Runs the action
  67. *
  68. * @return string result content
  69. */
  70. public function run()
  71. {
  72. if (($exception = Yii::$app->getErrorHandler()->exception) === null) {
  73. // action has been invoked not from error handler, but by direct route, so we display '404 Not Found'
  74. $exception = new HttpException(404, Yii::t('yii', 'Page not found.'));
  75. }
  76. if ($exception instanceof HttpException) {
  77. $code = $exception->statusCode;
  78. } else {
  79. $code = $exception->getCode();
  80. }
  81. if ($exception instanceof Exception) {
  82. $name = $exception->getName();
  83. } else {
  84. $name = $this->defaultName ?: Yii::t('yii', 'Error');
  85. }
  86. if ($code) {
  87. $name .= " (#$code)";
  88. }
  89. if ($exception instanceof UserException) {
  90. $message = $exception->getMessage();
  91. } else {
  92. $message = $this->defaultMessage ?: Yii::t('yii', 'An internal server error occurred.');
  93. }
  94. if (Yii::$app->getRequest()->getIsAjax()) {
  95. return "$name: $message";
  96. } else {
  97. return $this->controller->render($this->view ?: $this->id, [
  98. 'name' => $name,
  99. 'message' => $message,
  100. 'exception' => $exception,
  101. ]);
  102. }
  103. }
  104. }