LoginSignup
8
6

More than 5 years have passed since last update.

[baserCMS]Errorページの手軽な準備方法

Last updated at Posted at 2014-07-12

存在しないURLにアクセスがあった場合やコントローラやアクションのエラーが起きた場合に表示されるエラーページを用意する際は、以下のようにするとお手軽です。

環境

  • baserCMS 3.0.6.1
  • PHP 5.4.19

ファイルの作成

  • /theme/THEME_NAME/Errors/error404.php を作成する
error404.php
<?php
/**
 * [PUBLISH] 404エラー
 *
 */
//if (strpos($message, '.html') !== false)
//  $message = str_replace('pages/', '', $message);
$messageText = $this->response->statusCode() . ' ' . $message;
?>
<div id="errorPage">
    <h2><?php echo $messageText; ?></h2>
    <p class="error">
        <?php printf(
            __d('cake', 'The request sent to the address %s was invalid.'),
            "<strong>'{$url}'</strong>"
        ); ?>
    </p>
</div>

<?php if (Configure::read('debug') > 0): ?>
    <?php echo $this->element('exception_stack_trace'); ?>
<?php endif; ?>

次に、以下のようにファイルを作成します。

  • /theme/THEME_NAME/Errors/error400.php
  • /theme/THEME_NAME/Errors/error500.php
  • /theme/THEME_NAME/Errors/missing_action.php
  • /theme/THEME_NAME/Errors/missing_class.php
  • /theme/THEME_NAME/Errors/missing_controller.php

中身はこんな感じ。全部同じで良いです。

error500.php
<?php
/**
 * Error
 */
?>
<?php
include BASER_THEMES . $this->theme . DS . 'Errors' . DS . 'error404' . $this->ext;

エラー時に表示されるのは 404.php に統一。
表示はこのファイルを調整するだけでOKです。

閲覧している一般のユーザー側にController Errorとか出しても困惑しちゃうだろうし。。。って感じです。
正しいステータスコードを返すように変更しました。
エラー出たら /app/tmp/logs の中身を見たりしてガンバって解決しましょー。

ちなみに、大元のビューは以下に在ります。

  • /lib/Baser/View/Errors

併せて使うときがある情報

エラー出たらトップに飛ばすとか。

<?php
header('location: /', true, 301);
exit();

同じだけど違う書き方。

header('HTTP/1.1 301 Moved Permanently');
header('Location: /');
exit();
8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6