質問編集履歴
3
userscontroller.phpを掲載
test
CHANGED
File without changes
|
test
CHANGED
@@ -46,7 +46,7 @@
|
|
46
46
|
|
47
47
|
どなたか、アドバイスいただけたらありがたいです。
|
48
48
|
|
49
|
-
|
49
|
+
UsersController.php
|
50
50
|
|
51
51
|
```<?php
|
52
52
|
|
2
userscontroller.phpを掲載
test
CHANGED
File without changes
|
test
CHANGED
@@ -45,3 +45,285 @@
|
|
45
45
|
|
46
46
|
|
47
47
|
どなたか、アドバイスいただけたらありがたいです。
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
```<?php
|
52
|
+
|
53
|
+
declare(strict_types=1);
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
namespace App\Controller\Admin;
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
use App\Controller\Admin\AdminController;
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
/**
|
66
|
+
|
67
|
+
* Users Controller
|
68
|
+
|
69
|
+
*
|
70
|
+
|
71
|
+
* @property \App\Model\Table\UsersTable $Users
|
72
|
+
|
73
|
+
* @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
|
74
|
+
|
75
|
+
*/
|
76
|
+
|
77
|
+
class UsersController extends AdminController
|
78
|
+
|
79
|
+
{
|
80
|
+
|
81
|
+
public function beforeFilter(\Cake\Event\EventInterface $event)
|
82
|
+
|
83
|
+
{
|
84
|
+
|
85
|
+
parent::beforeFilter($event);
|
86
|
+
|
87
|
+
// ログインページは認証しなくてもアクセスできる
|
88
|
+
|
89
|
+
$this->Authentication->addUnauthenticatedActions(['login']);
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
/**
|
94
|
+
|
95
|
+
* Index method
|
96
|
+
|
97
|
+
*
|
98
|
+
|
99
|
+
* @return \Cake\Http\Response|null|void Renders view
|
100
|
+
|
101
|
+
*/
|
102
|
+
|
103
|
+
public function index()
|
104
|
+
|
105
|
+
{
|
106
|
+
|
107
|
+
$users = $this->paginate($this->Users);
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
$this->set(compact('users'));
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
/**
|
118
|
+
|
119
|
+
* View method
|
120
|
+
|
121
|
+
*
|
122
|
+
|
123
|
+
* @param string|null $id User id.
|
124
|
+
|
125
|
+
* @return \Cake\Http\Response|null|void Renders view
|
126
|
+
|
127
|
+
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
|
128
|
+
|
129
|
+
*/
|
130
|
+
|
131
|
+
public function view($id = null)
|
132
|
+
|
133
|
+
{
|
134
|
+
|
135
|
+
$user = $this->Users->get($id, [
|
136
|
+
|
137
|
+
'contain' => [],
|
138
|
+
|
139
|
+
]);
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
$this->set(compact('user'));
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
/**
|
150
|
+
|
151
|
+
* Add method
|
152
|
+
|
153
|
+
*
|
154
|
+
|
155
|
+
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
|
156
|
+
|
157
|
+
*/
|
158
|
+
|
159
|
+
public function add()
|
160
|
+
|
161
|
+
{
|
162
|
+
|
163
|
+
$user = $this->Users->newEmptyEntity();
|
164
|
+
|
165
|
+
if ($this->request->is('post')) {
|
166
|
+
|
167
|
+
$user = $this->Users->patchEntity($user, $this->request->getData());
|
168
|
+
|
169
|
+
if ($this->Users->save($user)) {
|
170
|
+
|
171
|
+
$this->Flash->success(__('The user has been saved.'));
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
return $this->redirect(['action' => 'index']);
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
$this->Flash->error(__('The user could not be saved. Please, try again.'));
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
$this->set(compact('user'));
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
/**
|
190
|
+
|
191
|
+
* Edit method
|
192
|
+
|
193
|
+
*
|
194
|
+
|
195
|
+
* @param string|null $id User id.
|
196
|
+
|
197
|
+
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
|
198
|
+
|
199
|
+
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
|
200
|
+
|
201
|
+
*/
|
202
|
+
|
203
|
+
public function edit($id = null)
|
204
|
+
|
205
|
+
{
|
206
|
+
|
207
|
+
$user = $this->Users->get($id, [
|
208
|
+
|
209
|
+
'contain' => [],
|
210
|
+
|
211
|
+
]);
|
212
|
+
|
213
|
+
if ($this->request->is(['patch', 'post', 'put'])) {
|
214
|
+
|
215
|
+
$user = $this->Users->patchEntity($user, $this->request->getData());
|
216
|
+
|
217
|
+
if ($this->Users->save($user)) {
|
218
|
+
|
219
|
+
$this->Flash->success(__('The user has been saved.'));
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
return $this->redirect(['action' => 'index']);
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
$this->Flash->error(__('The user could not be saved. Please, try again.'));
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
$this->set(compact('user'));
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
/**
|
238
|
+
|
239
|
+
* Delete method
|
240
|
+
|
241
|
+
*
|
242
|
+
|
243
|
+
* @param string|null $id User id.
|
244
|
+
|
245
|
+
* @return \Cake\Http\Response|null|void Redirects to index.
|
246
|
+
|
247
|
+
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
|
248
|
+
|
249
|
+
*/
|
250
|
+
|
251
|
+
public function delete($id = null)
|
252
|
+
|
253
|
+
{
|
254
|
+
|
255
|
+
$this->request->allowMethod(['post', 'delete']);
|
256
|
+
|
257
|
+
$user = $this->Users->get($id);
|
258
|
+
|
259
|
+
if ($this->Users->delete($user)) {
|
260
|
+
|
261
|
+
$this->Flash->success(__('The user has been deleted.'));
|
262
|
+
|
263
|
+
} else {
|
264
|
+
|
265
|
+
$this->Flash->error(__('The user could not be deleted. Please, try again.'));
|
266
|
+
|
267
|
+
}
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
return $this->redirect(['action' => 'index']);
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
public function login()
|
278
|
+
|
279
|
+
{
|
280
|
+
|
281
|
+
$this->request->allowMethod(['get', 'post']);
|
282
|
+
|
283
|
+
$result = $this->Authentication->getResult();
|
284
|
+
|
285
|
+
// ログインしている場合はリダイレクト
|
286
|
+
|
287
|
+
if ($result->isValid()) {
|
288
|
+
|
289
|
+
return $this->redirect('/admin');
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
// 認証失敗した場合は、エラーを表示します
|
294
|
+
|
295
|
+
if ($this->request->is('post') && !$result->isValid()) {
|
296
|
+
|
297
|
+
$this->Flash->error('ユーザー名かパスワードが正しくありません。');
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
public function logout()
|
306
|
+
|
307
|
+
{
|
308
|
+
|
309
|
+
$result = $this->Authentication->getResult();
|
310
|
+
|
311
|
+
// ログインした場合はリダイレクト
|
312
|
+
|
313
|
+
if ($result->isValid()) {
|
314
|
+
|
315
|
+
$this->Authentication->logout();
|
316
|
+
|
317
|
+
return $this->redirect(['controller' => 'Users', 'action' => 'login']);
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
}
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
```
|
1
ファイルの階層を掲載
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
![![イメージ説明](2be110def60f1672835ca3196849db16.png)]
|
2
|
+
|
3
|
+
下記のサイトを参考にして
|
4
|
+
|
1
5
|
[リンク内容](https://www.webopixel.net/cakephp/1612.html)
|
2
6
|
|
3
7
|
|