LoginSignup
0

More than 5 years have passed since last update.

OctoberCMSテーマ作成:ページPHPコードからPage Not Found (404)を返す

Last updated at Posted at 2018-05-13

OctoberCMSでは今のところ残念なことにLaravelのabort(404)が使えません。
404エラーページを表示せずに500エラーとして処理されてしまいます。

フロントエンドのページで404エラーページを表示するには下記の方法が考えられます。
1. /404 へリダイレクトさせる
2. コンポーネントで404のレスポンスを返させる
3. ページのPHPセクションで404のレスポンスを返させる

ここでは3について説明します。

ページテンプレートのPHPセクションで return Response::make($this->controller->run('404'), 404); を記述することでできます。

下記実装例では、URLで指定したプロダクトが存在しない場合Page not foundを表示させています。

url = "/product/:product_key"

[product]
product_key = "{{ :product_key }}"
==
<?php
function onInit()
{
    if (!$this->product->exist()) {
        return Response::make($this->controller->run('404'), 404);
    }
}
?>
==
{# テンプレートコード #}

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
0