回答編集履歴

1

CakePHPの環境作って確かになったので引っかかるところを記載するよう修正

2018/11/16 16:18

投稿

rururu3
rururu3

スコア5545

test CHANGED
@@ -1,13 +1,209 @@
1
- CakePHPあまり詳しくですが、
2
-
3
- Appがプロジェクト名たら
4
-
5
- `http://サイト名/App/Sample/Sample1/Review/loading`では?
6
-
7
-
8
-
9
- ※ReviewControllerクラスのPHPの最初にある
10
-
11
- `Controller/Sample/Sample1/loading`
12
-
13
- ってなんだろう
1
+ CakePHP3からControllerの階層化が面倒にったんですね・・・
2
+
3
+ CakePHP3.6.5で試しましたので引っかかった部分け記載ていきます
4
+
5
+
6
+
7
+ ※bin/cake server -H 0.0.0.0 -p 5673
8
+
9
+ で試してます
10
+
11
+
12
+
13
+ routers.php
14
+
15
+ ```php
16
+
17
+ // ファイルの最後に追加(ルーティング定義しないとだめになったらしい)
18
+
19
+ Router::prefix('Sample', function ($routes) {
20
+
21
+ $routes->fallbacks(DashedRoute::class);
22
+
23
+ });
24
+
25
+ ```
26
+
27
+ Application.php
28
+
29
+ ```php
30
+
31
+ ->add(new RoutingMiddleware($this, '_cake_routes_'));
32
+
33
+
34
+
35
+ // CSRFとりあえず面倒なのでコメント&↑部分文法合うよう;を入れる
36
+
37
+ // Add csrf middleware.
38
+
39
+ // ->add(new CsrfProtectionMiddleware([
40
+
41
+ // 'httpOnly' => true
42
+
43
+ // ]));
44
+
45
+ ```
46
+
47
+
48
+
49
+ Ajaxページ(テストとして用意したページ)
50
+
51
+ ※jquery.jsはLayoutで読み込ませるようにしてます
52
+
53
+ ```ctp
54
+
55
+ <button id="button">ボタン</button>
56
+
57
+ <script>
58
+
59
+ $("#button").on("click",function(){
60
+
61
+ var test = "test";
62
+
63
+ execAjax(test);
64
+
65
+ });
66
+
67
+
68
+
69
+ function execAjax(test){
70
+
71
+ $.ajax({
72
+
73
+ type: "POST",
74
+
75
+ url: "/sample/sample1/loading", // URLはこうなった
76
+
77
+ data: {
78
+
79
+ "test" : test,
80
+
81
+ },
82
+
83
+ })
84
+
85
+ // Ajaxリクエストが成功した時発動
86
+
87
+ .done(function(response){
88
+
89
+ alert("成功");
90
+
91
+ alert(response);
92
+
93
+ })
94
+
95
+ // Ajaxリクエストが失敗した時発動
96
+
97
+ .fail(function(XMLHttpRequest, textStatus, errorThrown){
98
+
99
+ alert("失敗");
100
+
101
+ console.log("ajax通信に失敗しました");
102
+
103
+ console.log("XMLHttpRequest : " + XMLHttpRequest.status);
104
+
105
+ console.log("textStatus : " + textStatus);
106
+
107
+ console.log("errorThrown : " + errorThrown.message);
108
+
109
+ });
110
+
111
+ }
112
+
113
+ </script>
114
+
115
+ ```
116
+
117
+
118
+
119
+ Sample1Controller.php(bin/cake bake controller Sample1 --prefix Sampleで作ってloadingだけに削ぎ落とした)
120
+
121
+ ```PHP
122
+
123
+ <?php
124
+
125
+ namespace App\Controller\Sample;
126
+
127
+
128
+
129
+ use App\Controller\AppController;
130
+
131
+
132
+
133
+ /**
134
+
135
+ * Sample1 Controller
136
+
137
+ *
138
+
139
+ *
140
+
141
+ * @method \App\Model\Entity\Sample1[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
142
+
143
+ */
144
+
145
+ class Sample1Controller extends AppController
146
+
147
+ {
148
+
149
+ /**
150
+
151
+ * Initialize method
152
+
153
+ */
154
+
155
+ public function initialize()
156
+
157
+ {
158
+
159
+ parent::initialize();
160
+
161
+ $this->loadComponent('RequestHandler');
162
+
163
+ }
164
+
165
+
166
+
167
+ /**
168
+
169
+ * Loading method
170
+
171
+ * @return $test テストデータ
172
+
173
+ */
174
+
175
+ public function loading()
176
+
177
+ {
178
+
179
+ $this->autoRender = false;
180
+
181
+ if ($this->request->is('ajax')) {
182
+
183
+ // 送られてきたリクエストデータを取得する
184
+
185
+ $test = $this->request->getData('test');
186
+
187
+
188
+
189
+ // 必要な処理を実装していく
190
+
191
+
192
+
193
+ // 戻り値を返却する
194
+
195
+
196
+
197
+ echo json_encode($test);
198
+
199
+ }
200
+
201
+ }
202
+
203
+ }
204
+
205
+ ```
206
+
207
+
208
+
209
+ これで動作確認できました。