質問編集履歴
7
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
# 環境
|
2
2
|
- php 7.2
|
3
3
|
- cakephp3.6
|
4
|
+
- nginx
|
4
5
|
|
5
6
|
|
6
7
|
# 困っていること
|
7
8
|
`throw new NotFoundException();`しても、サーバ(nginx)のエラーページが表示される
|
9
|
+
また、存在しないURLにアクセスしてもnginxのエラーページが表示される
|
8
10
|
|
9
11
|
|
10
12
|
# やっていること
|
6
エラー追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -162,4 +162,22 @@
|
|
162
162
|
- [CakePHPで404などエラーページを表示](https://easyramble.com/cakephp-error-handling.html)
|
163
163
|
- [コントローラーから例外エラー(404・500)を投げる](https://cakephp3.com/%E3%82%B3%E3%83%B3%E3%83%88%E3%83%AD%E3%83%BC%E3%83%A9%E3%83%BC%E3%81%8B%E3%82%89%E4%BE%8B%E5%A4%96%E3%82%A8%E3%83%A9%E3%83%BC404%E3%83%BB500%E3%82%92%E6%8A%95%E3%81%92%E3%82%8B)
|
164
164
|
|
165
|
-
英語のページも漁ってみたのですが、twigを使った方法がなく……困っております。
|
165
|
+
英語のページも漁ってみたのですが、twigを使った方法がなく……困っております。
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
# 追記
|
170
|
+
nginxのエラーです。
|
171
|
+
|
172
|
+
`/a`など、存在しないアドレスにアクセスした時です。
|
173
|
+
|
174
|
+
```
|
175
|
+
[Cake\Routing\Exception\MissingControllerException] Controller class A could not be found.
|
176
|
+
Exception Attributes: array (
|
177
|
+
'class' => 'A',
|
178
|
+
'plugin' => false,
|
179
|
+
'prefix' => false,
|
180
|
+
'_ext' => false,
|
181
|
+
)
|
182
|
+
Request URL: /a
|
183
|
+
```
|
5
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -145,10 +145,18 @@
|
|
145
145
|
{{ _view.end }}
|
146
146
|
```
|
147
147
|
|
148
|
-
---
|
148
|
+
---
|
149
149
|
|
150
|
+
上記コードを書いておりますが、
|
151
|
+
「`beforeRender`」メソッドまで処理が来ているのにもかかわらず、
|
152
|
+
画面がレンダリングされず、nginxのエラーページが表示されてしまします。
|
153
|
+
この原因がわからず、困っております。
|
154
|
+
おわかりの方、「多分これかな?」という心当たりのある方、
|
155
|
+
情報をいただけないでしょうか。
|
156
|
+
よろしくお願いいたします。
|
150
157
|
|
151
158
|
|
159
|
+
|
152
160
|
# 参考
|
153
161
|
- [CakePHPのエラーページをカスタマイズする](https://ameblo.jp/itboy/entry-11580790666.html)
|
154
162
|
- [CakePHPで404などエラーページを表示](https://easyramble.com/cakephp-error-handling.html)
|
4
質問自体を変更
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
CakePHP3.6 NotFoundExceptionで画面がレンダリングされない
|
body
CHANGED
@@ -3,41 +3,117 @@
|
|
3
3
|
- cakephp3.6
|
4
4
|
|
5
5
|
|
6
|
-
#
|
6
|
+
# 困っていること
|
7
|
-
Cakephp3でTwigを使っているのですが、
|
8
|
-
|
7
|
+
`throw new NotFoundException();`しても、サーバ(nginx)のエラーページが表示される
|
9
8
|
|
10
|
-
Cakephpでのtwigの呼び出しは以下を利用
|
11
|
-
[https://github.com/WyriHaximus/TwigView](https://github.com/WyriHaximus/TwigView)
|
12
9
|
|
10
|
+
# やっていること
|
13
11
|
|
14
|
-
|
12
|
+
以下の設定を行っております。
|
15
|
-
|
13
|
+
[※参考にしたサイト様](http://moblog.absgexp.net/cakeerr/#i)
|
16
14
|
|
17
|
-
- `error400.twig`
|
18
|
-
- `error500.twig`
|
19
15
|
|
20
|
-
**
|
16
|
+
**config/app.php**
|
21
17
|
|
22
|
-
```php
|
23
|
-
{{ _view.layout('error')|raw }}
|
24
|
-
not found
|
25
18
|
```
|
19
|
+
'Error' => [
|
20
|
+
'errorLevel' => E_ALL,
|
21
|
+
'exceptionRenderer' => 'Cake\Error\ExceptionRenderer',
|
22
|
+
'skipLog' => [],
|
23
|
+
'log' => true,
|
24
|
+
'trace' => true,
|
25
|
+
],
|
26
|
+
```
|
26
27
|
|
27
|
-
|
28
|
+
---
|
28
29
|
|
30
|
+
**Controller/ErrorController.php**
|
31
|
+
|
29
32
|
```php
|
33
|
+
<?php
|
34
|
+
/**
|
35
|
+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
36
|
+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
37
|
+
*
|
38
|
+
* Licensed under The MIT License
|
39
|
+
* For full copyright and license information, please see the LICENSE.txt
|
40
|
+
* Redistributions of files must retain the above copyright notice.
|
41
|
+
*
|
42
|
+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
43
|
+
* @link https://cakephp.org CakePHP(tm) Project
|
44
|
+
* @since 3.3.4
|
45
|
+
* @license https://opensource.org/licenses/mit-license.php MIT License
|
46
|
+
*/
|
47
|
+
namespace App\Controller;
|
48
|
+
|
49
|
+
use Cake\Controller\Component\AuthComponent;
|
50
|
+
use Cake\Event\Event;
|
51
|
+
|
52
|
+
/**
|
53
|
+
* Error Handling Controller
|
54
|
+
*
|
55
|
+
* Controller used by ExceptionRenderer to render error responses.
|
56
|
+
*/
|
57
|
+
class ErrorController extends AppController
|
58
|
+
{
|
59
|
+
/**
|
60
|
+
* Initialization hook method.
|
61
|
+
*
|
62
|
+
* @return void
|
63
|
+
* @throws \Exception
|
64
|
+
*/
|
65
|
+
public function initialize()
|
66
|
+
{
|
67
|
+
$this->loadComponent('RequestHandler');
|
30
|
-
|
68
|
+
$this->loadComponent('Auth');
|
69
|
+
|
70
|
+
$this->Auth->allow();
|
71
|
+
}
|
72
|
+
|
73
|
+
/**
|
74
|
+
* beforeFilter callback.
|
75
|
+
*
|
76
|
+
* @param \Cake\Event\Event $event Event.
|
77
|
+
* @return \Cake\Http\Response|null|void
|
78
|
+
*/
|
79
|
+
public function beforeFilter(Event $event)
|
80
|
+
{
|
81
|
+
}
|
82
|
+
|
83
|
+
/**
|
84
|
+
* beforeRender callback.
|
85
|
+
*
|
86
|
+
* @param \Cake\Event\Event $event Event.
|
87
|
+
* @return \Cake\Http\Response|null|void
|
88
|
+
*/
|
89
|
+
public function beforeRender(Event $event)
|
90
|
+
{
|
91
|
+
parent::beforeRender($event);
|
92
|
+
$this->viewBuilder()->setTemplatePath('Error');
|
93
|
+
}
|
94
|
+
|
95
|
+
/**
|
31
|
-
|
96
|
+
* afterFilter callback.
|
97
|
+
*
|
98
|
+
* @param \Cake\Event\Event $event Event.
|
99
|
+
* @return \Cake\Http\Response|null|void
|
100
|
+
*/
|
101
|
+
public function afterFilter(Event $event)
|
102
|
+
{
|
103
|
+
}
|
104
|
+
}
|
105
|
+
|
32
106
|
```
|
33
107
|
|
34
|
-
**Template/Layout/error.twig**
|
35
108
|
|
109
|
+
---
|
110
|
+
|
111
|
+
**Template/Layout/default.ctp**
|
112
|
+
|
36
|
-
```
|
113
|
+
```
|
37
114
|
<!DOCTYPE html>
|
38
115
|
<html>
|
39
116
|
<head>
|
40
|
-
{{ Html.charset() | raw }}
|
41
117
|
<title>
|
42
118
|
エラーページ
|
43
119
|
</title>
|
@@ -45,12 +121,10 @@
|
|
45
121
|
<body>
|
46
122
|
<div id="container">
|
47
123
|
<div id="header">
|
48
|
-
<h1>
|
124
|
+
<h1>エラー</h1>
|
49
125
|
</div>
|
50
126
|
<div id="content">
|
51
|
-
{{ flash.render() }}
|
52
|
-
|
53
|
-
{{ _view.fetch('content') }}
|
127
|
+
{{ _view.fetch('content')|raw }}
|
54
128
|
</div>
|
55
129
|
<div id="footer">
|
56
130
|
</div>
|
@@ -59,50 +133,25 @@
|
|
59
133
|
</html>
|
60
134
|
```
|
61
135
|
|
62
|
-
その後、エラー処理部分で、
|
63
|
-
`throw new NotFoundException();`を書きました。
|
64
|
-
もちろん`use Cake\Http\Exception\NotFoundException;`を先頭に書いております。
|
65
136
|
|
66
|
-
|
137
|
+
---
|
67
138
|
|
68
|
-
エラーページはtwigが使えないのかと思い、
|
69
139
|
|
70
|
-
|
140
|
+
**Template/Error/error400.twig**
|
71
|
-
- `error500.ctp`
|
72
141
|
|
142
|
+
```
|
73
|
-
|
143
|
+
{{ _view.start('main') }}
|
144
|
+
not found
|
145
|
+
{{ _view.end }}
|
146
|
+
```
|
74
147
|
|
148
|
+
---。
|
75
149
|
|
76
|
-
# 質問
|
77
|
-
0. cakephp3のtwigでカスタムエラーページの作成方法はこれであっているのでしょうか?
|
78
|
-
0. nginxで足りていない設定があるのでしょうか?
|
79
150
|
|
80
151
|
|
81
|
-
以上、ご存知の方がいらっしゃいましたら、ご教示いただけますと幸いです。
|
82
|
-
|
83
|
-
|
84
|
-
|
85
152
|
# 参考
|
86
153
|
- [CakePHPのエラーページをカスタマイズする](https://ameblo.jp/itboy/entry-11580790666.html)
|
87
154
|
- [CakePHPで404などエラーページを表示](https://easyramble.com/cakephp-error-handling.html)
|
88
155
|
- [コントローラーから例外エラー(404・500)を投げる](https://cakephp3.com/%E3%82%B3%E3%83%B3%E3%83%88%E3%83%AD%E3%83%BC%E3%83%A9%E3%83%BC%E3%81%8B%E3%82%89%E4%BE%8B%E5%A4%96%E3%82%A8%E3%83%A9%E3%83%BC404%E3%83%BB500%E3%82%92%E6%8A%95%E3%81%92%E3%82%8B)
|
89
156
|
|
90
|
-
英語のページも漁ってみたのですが、twigを使った方法がなく……困っております。
|
157
|
+
英語のページも漁ってみたのですが、twigを使った方法がなく……困っております。
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
# 追記
|
95
|
-
以下ページを参考にカスタムエラーページを作成してみたのですが、
|
96
|
-
`AppErrorController.php`で、
|
97
|
-
`$this->viewBuilder()->setTemplatePath('Error');`だとページがレンダリングされず、
|
98
|
-
`$this->viewBuilder()->templatePath('Error');`だと、ページが正常に表示されます。
|
99
|
-
|
100
|
-
ただ、`$this->viewBuilder()->templatePath('Error');`は`deprecated`になっています……
|
101
|
-
|
102
|
-
|
103
|
-
http://moblog.absgexp.net/cakeerr/#i-5
|
104
|
-
|
105
|
-
|
106
|
-
# 追記2
|
107
|
-
`deprecationWarning`を`$this->viewBuilder()->setTemplatePath('Error');`の上などに入れて、ワーニングメッセージを表示させると、こちらが指定したページが読み込まれる事がわかりました。
|
108
|
-
しかし、これがないとエラーページが表示されません。。。
|
3
追記2
title
CHANGED
File without changes
|
body
CHANGED
@@ -100,4 +100,9 @@
|
|
100
100
|
ただ、`$this->viewBuilder()->templatePath('Error');`は`deprecated`になっています……
|
101
101
|
|
102
102
|
|
103
|
-
http://moblog.absgexp.net/cakeerr/#i-5
|
103
|
+
http://moblog.absgexp.net/cakeerr/#i-5
|
104
|
+
|
105
|
+
|
106
|
+
# 追記2
|
107
|
+
`deprecationWarning`を`$this->viewBuilder()->setTemplatePath('Error');`の上などに入れて、ワーニングメッセージを表示させると、こちらが指定したページが読み込まれる事がわかりました。
|
108
|
+
しかし、これがないとエラーページが表示されません。。。
|
2
謎追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -87,4 +87,17 @@
|
|
87
87
|
- [CakePHPで404などエラーページを表示](https://easyramble.com/cakephp-error-handling.html)
|
88
88
|
- [コントローラーから例外エラー(404・500)を投げる](https://cakephp3.com/%E3%82%B3%E3%83%B3%E3%83%88%E3%83%AD%E3%83%BC%E3%83%A9%E3%83%BC%E3%81%8B%E3%82%89%E4%BE%8B%E5%A4%96%E3%82%A8%E3%83%A9%E3%83%BC404%E3%83%BB500%E3%82%92%E6%8A%95%E3%81%92%E3%82%8B)
|
89
89
|
|
90
|
-
英語のページも漁ってみたのですが、twigを使った方法がなく……困っております。
|
90
|
+
英語のページも漁ってみたのですが、twigを使った方法がなく……困っております。
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
# 追記
|
95
|
+
以下ページを参考にカスタムエラーページを作成してみたのですが、
|
96
|
+
`AppErrorController.php`で、
|
97
|
+
`$this->viewBuilder()->setTemplatePath('Error');`だとページがレンダリングされず、
|
98
|
+
`$this->viewBuilder()->templatePath('Error');`だと、ページが正常に表示されます。
|
99
|
+
|
100
|
+
ただ、`$this->viewBuilder()->templatePath('Error');`は`deprecated`になっています……
|
101
|
+
|
102
|
+
|
103
|
+
http://moblog.absgexp.net/cakeerr/#i-5
|
1
error.twig追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -31,6 +31,34 @@
|
|
31
31
|
server error
|
32
32
|
```
|
33
33
|
|
34
|
+
**Template/Layout/error.twig**
|
35
|
+
|
36
|
+
```php
|
37
|
+
<!DOCTYPE html>
|
38
|
+
<html>
|
39
|
+
<head>
|
40
|
+
{{ Html.charset() | raw }}
|
41
|
+
<title>
|
42
|
+
エラーページ
|
43
|
+
</title>
|
44
|
+
</head>
|
45
|
+
<body>
|
46
|
+
<div id="container">
|
47
|
+
<div id="header">
|
48
|
+
<h1>{{ __('Error') }}</h1>
|
49
|
+
</div>
|
50
|
+
<div id="content">
|
51
|
+
{{ flash.render() }}
|
52
|
+
|
53
|
+
{{ _view.fetch('content') }}
|
54
|
+
</div>
|
55
|
+
<div id="footer">
|
56
|
+
</div>
|
57
|
+
</div>
|
58
|
+
</body>
|
59
|
+
</html>
|
60
|
+
```
|
61
|
+
|
34
62
|
その後、エラー処理部分で、
|
35
63
|
`throw new NotFoundException();`を書きました。
|
36
64
|
もちろん`use Cake\Http\Exception\NotFoundException;`を先頭に書いております。
|