質問するログイン新規登録

質問編集履歴

2

追記

2021/01/18 07:02

投稿

meruchaaan
meruchaaan

スコア18

title CHANGED
File without changes
body CHANGED
@@ -14,6 +14,13 @@
14
14
  というようなエラーメッセージが吐かれます。
15
15
  このエラーメッセージで検索を行いましたが見当はずれのサイトばかりなのか自分の理解力がないのかで全く参考になるサイトがありませんでした。このエラーメッセージの原因と打開策をご教授お願い致します。
16
16
 
17
+ 自身の見解は、controllerphpのlogin関数でlogメソッドで確認したところ
18
+ > Argument 1 passed to Cake\Controller\Controller::log() must be of the type string, object given, called in /var/www/html/sample/src/Controller/ContactsController.php on line 163
19
+
20
+ というエラーメッセージが吐かれていたのでlogin関数の$resultになにもデータが入っていないことが原因だと考えていますがどこが問題でデータがとれていないのでしょうか?
21
+ 宜しくお願い致します。
22
+
23
+
17
24
  ```applicationphp
18
25
  <?php
19
26
  declare(strict_types=1);

1

足りない情報の補足

2021/01/18 07:02

投稿

meruchaaan
meruchaaan

スコア18

title CHANGED
@@ -1,1 +1,1 @@
1
- cakephp4 ログイン認証機能 Table class for alias Users could not be found.エラー
1
+ cakephp4 ログイン認証機能について
body CHANGED
@@ -4,26 +4,281 @@
4
4
  Authentication 2.0
5
5
 
6
6
  参考にしたサイト↓↓↓↓↓↓↓↓↓↓
7
- [CakePHP 4 で Authentication プラグインを使ってユーザー認証を実装
8
- ](https://tt-computing.com/cake4-authc)
7
+ [シンプルな認証と認可のアプリケーション](https://book.cakephp.org/4/ja/tutorials-and-examples/blog-auth-example/auth.html)
9
8
 
10
- しかし問題が二点発生しました。
11
- 一つ目は上記のサイトの通り下記のように記述するとリダイレクトループが起こりエラーになることです。
12
- し、下記の記述をなくすそのエラはなくなるため現在は記述ておりません。
9
+ ログインページら名前パスワドを入力ログインしようとすると、
10
+
11
+ > Table class for alias Users could not be found.
12
+ エイリアスUsersのテーブルクラスが見つかりませんでした。
13
+
13
- こちら一つ目の質問部分です。
14
+ というようなエラーメッセージ吐かれます。
15
+ このエラーメッセージで検索を行いましたが見当はずれのサイトばかりなのか自分の理解力がないのかで全く参考になるサイトがありませんでした。このエラーメッセージの原因と打開策をご教授お願い致します。
16
+
14
- ```AppControllerphp
17
+ ```applicationphp
18
+ <?php
19
+ declare(strict_types=1);
20
+
21
+ /**
22
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
23
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
24
+ *
25
+ * Licensed under The MIT License
26
+ * For full copyright and license information, please see the LICENSE.txt
27
+ * Redistributions of files must retain the above copyright notice.
28
+ *
29
+ * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
30
+ * @link https://cakephp.org CakePHP(tm) Project
31
+ * @since 3.3.0
32
+ * @license https://opensource.org/licenses/mit-license.php MIT License
33
+ */
34
+ namespace App;
35
+
36
+ use Cake\Core\Configure;
37
+ use Cake\Core\ContainerInterface;
38
+ use Cake\Core\Exception\MissingPluginException;
39
+ use Cake\Datasource\FactoryLocator;
40
+ use Cake\Error\Middleware\ErrorHandlerMiddleware;
41
+ use Cake\Http\BaseApplication;
42
+ use Cake\Http\Middleware\BodyParserMiddleware;
43
+ use Cake\Http\Middleware\CsrfProtectionMiddleware;
44
+ use Cake\Http\MiddlewareQueue;
45
+ use Cake\ORM\Locator\TableLocator;
46
+ use Cake\Routing\Middleware\AssetMiddleware;
47
+ use Cake\Routing\Middleware\RoutingMiddleware;
15
- public function initialize(): void
48
+ use Authentication\AuthenticationService;
49
+ use Authentication\AuthenticationServiceInterface;
50
+ use Authentication\AuthenticationServiceProviderInterface;
51
+ use Authentication\Middleware\AuthenticationMiddleware;
52
+ use Cake\Routing\Router;
53
+ use Psr\Http\Message\ServerRequestInterface;
54
+
55
+
56
+ /**
57
+ * Application setup class.
58
+ *
59
+ * This defines the bootstrapping logic and middleware layers you
60
+ * want to use in your application.
61
+ */
62
+ class Application extends BaseApplication implements AuthenticationServiceProviderInterface
16
63
  {
64
+ /**
65
+ * Load all the application configuration and bootstrap logic.
66
+ *
67
+ * @return void
68
+ */
69
+ public function bootstrap(): void
70
+ {
71
+ // Call parent to load bootstrap from files.
72
+ parent::bootstrap();
73
+
74
+ if (PHP_SAPI === 'cli') {
75
+ $this->bootstrapCli();
76
+ } else {
77
+ FactoryLocator::add(
78
+ 'Table',
79
+ (new TableLocator())->allowFallbackClass(false)
17
- ...
80
+ );
81
+ }
82
+
83
+ /*
84
+ * Only try to load DebugKit in development mode
85
+ * Debug Kit should not be installed on a production system
86
+ */
87
+ if (Configure::read('debug')) {
88
+ $this->addPlugin('DebugKit');
89
+ }
90
+
91
+ // Load more plugins here
18
- $this->loadComponent('Authentication.Authentication'); // ← 追加
92
+ $this->addPlugin('Authentication');
93
+ }
94
+
95
+ /**
96
+ * Setup the middleware queue your application will use.
97
+ *
98
+ * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup.
99
+ * @return \Cake\Http\MiddlewareQueue The updated middleware queue.
100
+ */
101
+ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
102
+ {
103
+ $middlewareQueue
104
+ // Catch any exceptions in the lower layers,
105
+ // and make an error page/response
106
+ ->add(new ErrorHandlerMiddleware(Configure::read('Error')))
107
+
108
+ // Handle plugin/theme assets like CakePHP normally does.
109
+ ->add(new AssetMiddleware([
110
+ 'cacheTime' => Configure::read('Asset.cacheTime'),
111
+ ]))
112
+
113
+ // Add routing middleware.
114
+ // If you have a large number of routes connected, turning on routes
115
+ // caching in production could improve performance. For that when
116
+ // creating the middleware instance specify the cache config name by
117
+ // using it's second constructor argument:
118
+ // `new RoutingMiddleware($this, '_cake_routes_')`
119
+ ->add(new RoutingMiddleware($this))
120
+
121
+ ->add(new AuthenticationMiddleware($this))
122
+
123
+
124
+ // Parse various types of encoded request bodies so that they are
125
+ // available as array through $request->getData()
126
+ // https://book.cakephp.org/4/en/controllers/middleware.html#body-parser-middleware
127
+ ->add(new BodyParserMiddleware())
128
+
129
+ // Cross Site Request Forgery (CSRF) Protection Middleware
130
+ // https://book.cakephp.org/4/en/controllers/middleware.html#cross-site-request-forgery-csrf-middleware
131
+ ->add(new CsrfProtectionMiddleware([
132
+ 'httponly' => true
133
+ ]));
134
+
135
+
136
+
137
+ return $middlewareQueue;
138
+ }
139
+
140
+ /**
141
+ * Register application container services.
142
+ *
143
+ * @param \Cake\Core\ContainerInterface $container The Container to update.
144
+ * @return void
145
+ * @link https://book.cakephp.org/4/en/development/dependency-injection.html#dependency-injection
146
+ */
147
+ public function services(ContainerInterface $container): void
148
+ {
149
+ }
150
+
151
+ /**
152
+ * Bootstrapping for CLI application.
153
+ *
154
+ * That is when running commands.
155
+ *
156
+ * @return void
157
+ */
158
+ protected function bootstrapCli(): void
159
+ {
160
+ try {
161
+ $this->addPlugin('Bake');
162
+ } catch (MissingPluginException $e) {
163
+ // Do not halt if the plugin is missing
164
+ }
165
+
166
+ $this->addPlugin('Migrations');
167
+
168
+ // Load more plugins here
169
+ }
170
+
171
+ public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
172
+ {
173
+ $authenticationService = new AuthenticationService([
174
+ 'unauthenticatedRedirect' => '/sample/contacts/login',
175
+ 'queryParam' => 'redirect',
176
+ ]);
177
+
178
+ // 識別子をロードして、電子メールとパスワードのフィールドを確認します
179
+ $authenticationService->loadIdentifier('Authentication.Password', [
180
+ 'fields' => [
181
+ 'username' => 'email',
182
+ 'password' => 'password',
183
+ ]
184
+ ]);
185
+
186
+ // 認証子をロードするには、最初にセッションを実行する必要があります
187
+ $authenticationService->loadAuthenticator('Authentication.Session');
188
+ // メールとパスワードを選択するためのフォームデータチェックの設定
189
+ $authenticationService->loadAuthenticator('Authentication.Form', [
190
+ 'fields' => [
191
+ 'username' => 'email',
192
+ 'password' => 'password',
193
+ ],
194
+ 'loginUrl' => '/sample/contacts/login',
195
+ ]);
196
+
197
+ return $authenticationService;
19
198
  }
199
+ }
200
+
20
201
  ```
21
202
 
203
+ ```controllerphp
22
- そして二つ目は上記サイトの通り記述を行い、ログインページから名前とパスワードを入力しログインしようとすると、
204
+ public function beforeFilter(\Cake\Event\EventInterface $event)
205
+ {
206
+ parent::beforeFilter($event);
207
+ $this->Authentication->allowUnauthenticated(['login', 'add']);
208
+ }
23
209
 
210
+ public function login()
211
+ {
212
+ $result = $this->Authentication->getResult();
213
+ debug($result, true);
214
+ // 認証成功
215
+ if ($result->isValid()) {
216
+ $target = $this->Authentication->getLoginRedirect() ?? '/home0';
217
+ return $this->redirect($target);
218
+ }
219
+ // ログインできなかった場合
220
+ if ($this->request->is('post') && !$result->isValid()) {
24
- > Table class for alias Users could not be found.
221
+ $this->Flash->error('Invalid username or password');
222
+ }
223
+ }
25
- エイリアスUsersのテーブルクラスが見つかりませんでした。
224
+ ```
26
225
 
226
+ ```appcontroller
227
+ <?php
228
+ declare(strict_types=1);
229
+
230
+ /**
231
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
232
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
233
+ *
234
+ * Licensed under The MIT License
235
+ * For full copyright and license information, please see the LICENSE.txt
236
+ * Redistributions of files must retain the above copyright notice.
237
+ *
238
+ * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
239
+ * @link https://cakephp.org CakePHP(tm) Project
240
+ * @since 0.2.9
241
+ * @license https://opensource.org/licenses/mit-license.php MIT License
242
+ */
243
+ namespace App\Controller;
244
+
245
+ use Cake\Controller\Controller;
246
+ use Cake\ORM\TableRegistry;
247
+
248
+ /**
249
+ * Application Controller
250
+ *
251
+ * Add your application-wide methods in the class below, your controllers
252
+ * will inherit them.
253
+ *
254
+ * @link https://book.cakephp.org/4/en/controllers.html#the-app-controller
255
+ */
256
+ class AppController extends Controller
257
+ {
258
+ /**
259
+ * Initialization hook method.
260
+ *
261
+ * Use this method to add common initialization code like loading components.
262
+ *
263
+ * e.g. `$this->loadComponent('FormProtection');`
264
+ *
265
+ * @return void
266
+ */
267
+ public function initialize(): void
268
+ {
27
- というようなエラーメッセージが吐かれます。
269
+ parent::initialize();
270
+
271
+ $this->Contacts = TableRegistry::get('contacts');
272
+
273
+ $this->loadComponent('RequestHandler');
274
+ $this->loadComponent('Flash');
28
- このエラーメッセージで検索を行いましたが見当はずれのサイトばかりなのか自分の理解力がないのかで全く参考になるサイトがありませんでした。エイリアスとは別名というような意味という感じはわかりましたが、具体的な使われ方などがわからないためお聞きしたいです。そしてこのエラーメッセージの原因と打開策をご教授
275
+ $this->loadComponent('Authentication.Authentication');
29
- お願い致します。
276
+ /*
277
+ * Enable the following component for recommended CakePHP form protection settings.
278
+ * see https://book.cakephp.org/4/en/controllers/components/form-protection.html
279
+ */
280
+ //$this->loadComponent('FormProtection');
281
+ }
282
+ }
283
+
284
+ ```