質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

ログイン

ログインは、ユーザーがコンピューターシステムにアクセスするプロセスの事を呼びます。

Authentication

Authentication(認証)は正当性を認証する為の工程です。ログイン処理等で使われます。

Q&A

解決済

1回答

6247閲覧

cakephp4 ログイン認証機能について

meruchaaan

総合スコア18

CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

ログイン

ログインは、ユーザーがコンピューターシステムにアクセスするプロセスの事を呼びます。

Authentication

Authentication(認証)は正当性を認証する為の工程です。ログイン処理等で使われます。

0グッド

0クリップ

投稿2021/01/16 06:49

編集2021/01/18 07:02

現在私はcakephp4でログイン認証機能を作ろうと試みております。

cakephp4
Authentication 2.0

参考にしたサイト↓↓↓↓↓↓↓↓↓↓
シンプルな認証と認可のアプリケーション

ログインページから名前とパスワードを入力しログインしようとすると、

Table class for alias Users could not be found.

エイリアスUsersのテーブルクラスが見つかりませんでした。

というようなエラーメッセージが吐かれます。
このエラーメッセージで検索を行いましたが見当はずれのサイトばかりなのか自分の理解力がないのかで全く参考になるサイトがありませんでした。このエラーメッセージの原因と打開策をご教授お願い致します。

自身の見解は、controllerphpのlogin関数でlogメソッドで確認したところ

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

というエラーメッセージが吐かれていたのでlogin関数の$resultになにもデータが入っていないことが原因だと考えていますがどこが問題でデータがとれていないのでしょうか?
宜しくお願い致します。

applicationphp

1<?php 2declare(strict_types=1); 3 4/** 5 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org) 6 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) 7 * 8 * Licensed under The MIT License 9 * For full copyright and license information, please see the LICENSE.txt 10 * Redistributions of files must retain the above copyright notice. 11 * 12 * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) 13 * @link https://cakephp.org CakePHP(tm) Project 14 * @since 3.3.0 15 * @license https://opensource.org/licenses/mit-license.php MIT License 16 */ 17namespace App; 18 19use Cake\Core\Configure; 20use Cake\Core\ContainerInterface; 21use Cake\Core\Exception\MissingPluginException; 22use Cake\Datasource\FactoryLocator; 23use Cake\Error\Middleware\ErrorHandlerMiddleware; 24use Cake\Http\BaseApplication; 25use Cake\Http\Middleware\BodyParserMiddleware; 26use Cake\Http\Middleware\CsrfProtectionMiddleware; 27use Cake\Http\MiddlewareQueue; 28use Cake\ORM\Locator\TableLocator; 29use Cake\Routing\Middleware\AssetMiddleware; 30use Cake\Routing\Middleware\RoutingMiddleware; 31use Authentication\AuthenticationService; 32use Authentication\AuthenticationServiceInterface; 33use Authentication\AuthenticationServiceProviderInterface; 34use Authentication\Middleware\AuthenticationMiddleware; 35use Cake\Routing\Router; 36use Psr\Http\Message\ServerRequestInterface; 37 38 39/** 40 * Application setup class. 41 * 42 * This defines the bootstrapping logic and middleware layers you 43 * want to use in your application. 44 */ 45class Application extends BaseApplication implements AuthenticationServiceProviderInterface 46{ 47 /** 48 * Load all the application configuration and bootstrap logic. 49 * 50 * @return void 51 */ 52 public function bootstrap(): void 53 { 54 // Call parent to load bootstrap from files. 55 parent::bootstrap(); 56 57 if (PHP_SAPI === 'cli') { 58 $this->bootstrapCli(); 59 } else { 60 FactoryLocator::add( 61 'Table', 62 (new TableLocator())->allowFallbackClass(false) 63 ); 64 } 65 66 /* 67 * Only try to load DebugKit in development mode 68 * Debug Kit should not be installed on a production system 69 */ 70 if (Configure::read('debug')) { 71 $this->addPlugin('DebugKit'); 72 } 73 74 // Load more plugins here 75 $this->addPlugin('Authentication'); 76 } 77 78 /** 79 * Setup the middleware queue your application will use. 80 * 81 * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup. 82 * @return \Cake\Http\MiddlewareQueue The updated middleware queue. 83 */ 84 public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue 85 { 86 $middlewareQueue 87 // Catch any exceptions in the lower layers, 88 // and make an error page/response 89 ->add(new ErrorHandlerMiddleware(Configure::read('Error'))) 90 91 // Handle plugin/theme assets like CakePHP normally does. 92 ->add(new AssetMiddleware([ 93 'cacheTime' => Configure::read('Asset.cacheTime'), 94 ])) 95 96 // Add routing middleware. 97 // If you have a large number of routes connected, turning on routes 98 // caching in production could improve performance. For that when 99 // creating the middleware instance specify the cache config name by 100 // using it's second constructor argument: 101 // `new RoutingMiddleware($this, '_cake_routes_')` 102 ->add(new RoutingMiddleware($this)) 103 104 ->add(new AuthenticationMiddleware($this)) 105 106 107 // Parse various types of encoded request bodies so that they are 108 // available as array through $request->getData() 109 // https://book.cakephp.org/4/en/controllers/middleware.html#body-parser-middleware 110 ->add(new BodyParserMiddleware()) 111 112 // Cross Site Request Forgery (CSRF) Protection Middleware 113 // https://book.cakephp.org/4/en/controllers/middleware.html#cross-site-request-forgery-csrf-middleware 114 ->add(new CsrfProtectionMiddleware([ 115 'httponly' => true 116 ])); 117 118 119 120 return $middlewareQueue; 121 } 122 123 /** 124 * Register application container services. 125 * 126 * @param \Cake\Core\ContainerInterface $container The Container to update. 127 * @return void 128 * @link https://book.cakephp.org/4/en/development/dependency-injection.html#dependency-injection 129 */ 130 public function services(ContainerInterface $container): void 131 { 132 } 133 134 /** 135 * Bootstrapping for CLI application. 136 * 137 * That is when running commands. 138 * 139 * @return void 140 */ 141 protected function bootstrapCli(): void 142 { 143 try { 144 $this->addPlugin('Bake'); 145 } catch (MissingPluginException $e) { 146 // Do not halt if the plugin is missing 147 } 148 149 $this->addPlugin('Migrations'); 150 151 // Load more plugins here 152 } 153 154 public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface 155{ 156 $authenticationService = new AuthenticationService([ 157 'unauthenticatedRedirect' => '/sample/contacts/login', 158 'queryParam' => 'redirect', 159 ]); 160 161 // 識別子をロードして、電子メールとパスワードのフィールドを確認します 162 $authenticationService->loadIdentifier('Authentication.Password', [ 163 'fields' => [ 164 'username' => 'email', 165 'password' => 'password', 166 ] 167 ]); 168 169 // 認証子をロードするには、最初にセッションを実行する必要があります 170 $authenticationService->loadAuthenticator('Authentication.Session'); 171 // メールとパスワードを選択するためのフォームデータチェックの設定 172 $authenticationService->loadAuthenticator('Authentication.Form', [ 173 'fields' => [ 174 'username' => 'email', 175 'password' => 'password', 176 ], 177 'loginUrl' => '/sample/contacts/login', 178 ]); 179 180 return $authenticationService; 181} 182} 183

controllerphp

1public function beforeFilter(\Cake\Event\EventInterface $event) 2 { 3 parent::beforeFilter($event); 4 $this->Authentication->allowUnauthenticated(['login', 'add']); 5 } 6 7 public function login() 8 { 9 $result = $this->Authentication->getResult(); 10 debug($result, true); 11 // 認証成功 12 if ($result->isValid()) { 13 $target = $this->Authentication->getLoginRedirect() ?? '/home0'; 14 return $this->redirect($target); 15 } 16 // ログインできなかった場合 17 if ($this->request->is('post') && !$result->isValid()) { 18 $this->Flash->error('Invalid username or password'); 19 } 20 }

appcontroller

1<?php 2declare(strict_types=1); 3 4/** 5 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org) 6 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) 7 * 8 * Licensed under The MIT License 9 * For full copyright and license information, please see the LICENSE.txt 10 * Redistributions of files must retain the above copyright notice. 11 * 12 * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) 13 * @link https://cakephp.org CakePHP(tm) Project 14 * @since 0.2.9 15 * @license https://opensource.org/licenses/mit-license.php MIT License 16 */ 17namespace App\Controller; 18 19use Cake\Controller\Controller; 20use Cake\ORM\TableRegistry; 21 22/** 23 * Application Controller 24 * 25 * Add your application-wide methods in the class below, your controllers 26 * will inherit them. 27 * 28 * @link https://book.cakephp.org/4/en/controllers.html#the-app-controller 29 */ 30class AppController extends Controller 31{ 32 /** 33 * Initialization hook method. 34 * 35 * Use this method to add common initialization code like loading components. 36 * 37 * e.g. `$this->loadComponent('FormProtection');` 38 * 39 * @return void 40 */ 41 public function initialize(): void 42 { 43 parent::initialize(); 44 45 $this->Contacts = TableRegistry::get('contacts'); 46 47 $this->loadComponent('RequestHandler'); 48 $this->loadComponent('Flash'); 49 $this->loadComponent('Authentication.Authentication'); 50 /* 51 * Enable the following component for recommended CakePHP form protection settings. 52 * see https://book.cakephp.org/4/en/controllers/components/form-protection.html 53 */ 54 //$this->loadComponent('FormProtection'); 55 } 56} 57

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

$authenticationService->loadIdentifier('Authentication.Password', [

'resolver'=>[ 'className' => 'Authentication.Orm', 'userModel' => 'Contacts' ], 'fields' => [ 'username' => 'mail', 'password' => 'password', ] ]);

これでできました

投稿2021/01/18 08:02

meruchaaan

総合スコア18

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問