質問編集履歴

2

コードを書いた

2017/04/26 07:43

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -38,10 +38,244 @@
38
38
 
39
39
 
40
40
 
41
-
41
+ ```ここに言語を入力
42
+
43
+
44
+
42
-
45
+ UsersControllerのコードです。
46
+
47
+
48
+
49
+
50
+
43
-
51
+ <?php
52
+
53
+
54
+
55
+
56
+
57
+ App::uses('AppController', 'Controller');
58
+
59
+
60
+
61
+ class UsersController extends AppController {
62
+
63
+
64
+
65
+ //....
66
+
67
+
68
+
69
+ public function beforeFilter() {
70
+
71
+ parent::beforeFilter();
72
+
73
+ // ユーザー自身による登録とログアウトを許可する
74
+
75
+ $this->Auth->allow('add', 'logout');
76
+
77
+ }
78
+
79
+
80
+
81
+ public function login() {
82
+
83
+ if ($this->request->is('post')) {
84
+
85
+ if ($this->Auth->login()) {
86
+
87
+ $this->redirect($this->Auth->redirect());
88
+
89
+ } else {
90
+
91
+ $this->Flash->error(__('Invalid username or password, try again'));
92
+
93
+ }
94
+
95
+ }
96
+
97
+ }
98
+
99
+
100
+
101
+ public function logout() {
102
+
103
+ $this->redirect($this->Auth->logout());
104
+
105
+ }
106
+
107
+
108
+
109
+ public function index() {
110
+
111
+ $this->User->recursive = 0;
112
+
113
+ $this->set('users', $this->paginate());
114
+
115
+ }
116
+
117
+
118
+
119
+ public function view($id = null) {
120
+
121
+ $this->User->id = $id;
122
+
123
+ if (!$this->User->exists()) {
124
+
125
+ throw new NotFoundException(__('Invalid user'));
126
+
127
+ }
128
+
129
+ $this->set('user', $this->User->findById($id));
130
+
131
+ }
132
+
133
+
134
+
135
+ public function add() {
136
+
137
+ if ($this->request->is('post')) {
138
+
139
+ $this->User->create();
140
+
141
+ if ($this->User->save($this->request->data)) {
142
+
143
+ $this->Flash->success(__('The user has been saved'));
144
+
145
+ return $this->redirect(array('action' => 'index'));
146
+
147
+ }
148
+
149
+ $this->Flash->error(
150
+
151
+ __('The user could not be saved. Please, try again.')
152
+
153
+ );
154
+
155
+ }
156
+
157
+ }
158
+
159
+
160
+
161
+ public function edit($id = null) {
162
+
163
+ $this->User->id = $id;
164
+
165
+ if (!$this->User->exists()) {
166
+
167
+ throw new NotFoundException(__('Invalid user'));
168
+
169
+ }
170
+
171
+ if ($this->request->is('post') || $this->request->is('put')) {
172
+
173
+ if ($this->User->save($this->request->data)) {
174
+
175
+ $this->Flash->success(__('The user has been saved'));
176
+
177
+ return $this->redirect(array('action' => 'index'));
178
+
179
+ }
180
+
181
+ $this->Flash->error(
182
+
183
+ __('The user could not be saved. Please, try again.')
184
+
185
+ );
186
+
187
+ } else {
188
+
189
+ $this->request->data = $this->User->findById($id);
190
+
191
+ unset($this->request->data['User']['password']);
192
+
193
+ }
194
+
195
+ }
196
+
197
+
198
+
199
+ public function delete($id = null) {
200
+
201
+ // Prior to 2.5 use
202
+
203
+ // $this->request->onlyAllow('post');
204
+
205
+
206
+
207
+ $this->request->allowMethod('post');
208
+
209
+
210
+
211
+ $this->User->id = $id;
212
+
213
+ if (!$this->User->exists()) {
214
+
215
+ throw new NotFoundException(__('Invalid user'));
216
+
217
+ }
218
+
219
+ if ($this->User->delete()) {
220
+
221
+ $this->Flash->success(__('User deleted'));
222
+
223
+ return $this->redirect(array('action' => 'index'));
224
+
225
+ }
226
+
227
+ $this->Flash->error(__('User was not deleted'));
228
+
229
+ return $this->redirect(array('action' => 'index'));
230
+
231
+ }
232
+
233
+
234
+
235
+ }
236
+
237
+
238
+
239
+ ?>
44
240
 
45
241
 
46
242
 
47
243
  ```
244
+
245
+
246
+
247
+ add.ctpの中身です
248
+
249
+
250
+
251
+ <!-- app/View/Users/add.ctp -->
252
+
253
+ <div class="users form">
254
+
255
+ <?php echo $this->Form->create('User'); ?>
256
+
257
+ <fieldset>
258
+
259
+ <legend><?php echo __('Add User'); ?></legend>
260
+
261
+ <?php echo $this->Form->input('username');
262
+
263
+ echo $this->Form->input('password');
264
+
265
+ echo $this->Form->input('role', array(
266
+
267
+ 'options' => array('admin' => 'Admin', 'author' => 'Author')
268
+
269
+ ));
270
+
271
+ ?>
272
+
273
+ </fieldset>
274
+
275
+ <?php echo $this->Form->end(__('Submit')); ?>
276
+
277
+ </div>
278
+
279
+
280
+
281
+ ```

1

がぞうこうしん

2017/04/26 07:43

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -14,6 +14,8 @@
14
14
 
15
15
 
16
16
 
17
+ ![イメージ説明](9b288900fd69d1f07bb5e8230de41f06.jpeg)
18
+
17
19
  ```
18
20
 
19
21
  http://localhost/cake/users/add
@@ -25,10 +27,6 @@
25
27
  に入力しても、この画面のままになってしまい、
26
28
 
27
29
  記事の投稿などが出来ません。
28
-
29
-
30
-
31
- ![イメージ説明](a13dcbd7cc9bbc66ac22d8ca5e699af6.jpeg)
32
30
 
33
31
 
34
32