3

CakePHP を 2.6.2 から 2.7.2 に更新した後、認証フラッシュ メッセージが作成されるときにキーが見つからないというエラーが表示されます。デフォルトの要素テンプレートを定義するにはどうすればよいですauthErrorか?

SessionComponent::setFlash()廃止されたため、 FlashComponentを追加し、app/Controller/AppController.phpすべての Flash メッセージを次のように変更しました。

// Controller
$this->Session->setFlash('Done', 'succeed');
$this->Session->setFlash('There is an error', 'failure');
$this->Session->setFlash('Please log in', 'auth');
// View (default Layout)
echo $this->Session->flash();
echo $this->Session->flash('auth');

これに:

// Controller
$this->Flash->succeed('Done');
$this->Flash->failure('There is an error');
$this->Flash->auth('Please log in');
// View (default Layout)
echo $this->Flash->render();
echo $this->Session->flash();       // keep temporarily?
echo $this->Session->flash('auth'); // keep temporarily?

また、フラッシュ関連のテンプレートを から App/View/Elements/succeed.ctpに コピーしましたApp/View/Elements/Flash/succeed.ctp

これは機能していますログインせずに管理ページにアクセスしようとすると、テンプレートが表示されていないデフォルトのauthErrorメッセージ表示されます。デバッグ モード 2 では、次のエラーが発生します。app/Controller/AppController.php

// Undefined variable: key [CORE\Cake\View\Elements\Flash\default.ctp, line 1]
// include - CORE\Cake\View\Elements\Flash\default.ctp, line 1
// View::_evaluate() - CORE\Cake\View\View.php, line 971
// View::_render() - CORE\Cake\View\View.php, line 933
// View::_renderElement() - CORE\Cake\View\View.php, line 1227
// View::element() - CORE\Cake\View\View.php, line 418
// SessionHelper::flash() - CORE\Cake\View\Helper\SessionHelper.php, line 159
// include - APP\View\Layouts\default.ctp, line 142
// View::_evaluate() - CORE\Cake\View\View.php, line 971
// View::_render() - CORE\Cake\View\View.php, line 933
// View::renderLayout() - CORE\Cake\View\View.php, line 546
// View::render() - CORE\Cake\View\View.php, line 481
// Controller::render() - CORE\Cake\Controller\Controller.php, line 960
// Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 200
// Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 167
// [main] - APP\webroot\index.php, line 118
// Message" class="message">

独自の要素テンプレート「auth」でレンダリングされるデフォルトの authError を取得するには、AppController.php でどのような変更が必要ですか?

AppController.php の一部:

public $components = array(
  'Flash',
  'Session',
  'Security',
  'Auth' => array(
    'authenticate' => array('Form' => array('passwordHasher' => 'Blowfish')),
    'authError' => 'My default auth error message.', // How do I have to modify this line?
    'loginAction' => array('controller' => 'users', 'action' => 'login'),
    'loginRedirect' => array('controller' => 'users', 'action' => 'welcome'),
    'logoutRedirect' => array('controller' => 'users', 'action' => 'goodbye'),
  )
);

また、すべてのコントローラーのすべてのフラッシュ メッセージをフラッシュ コンポーネントとヘルパーに変更する場合、これらの 2 行はまだ必要ですか? それらは CakePHP によって他にどこで使用されますか?

echo $this->Session->flash();
echo $this->Session->flash('auth');

Authentication tutorialも見ました。$this->Session->setFlash()しかし、まだ頻繁に使用されているため、最新ではないようです...

4

4 に答える 4

2

Authコンポーネント設定配列に次のようなものを追加します

'Auth' = [
    ...
    'flash' => ['element' => 'auth_error'],
    ...
]

次に、ディレクトリに名前が付けられたテンプレートを作成auth_error.ctpしますElement/Flash。このファイルで使用する唯一の変数$message$key

たぶん、この答えは100%正しいとは限りません(したがって、どんな提案も歓迎します)が、私にとってはうまくいきました.

于 2015-08-17T08:41:37.810 に答える
0

私も同じ問題に直面しました。これは間違いなくあなたの問題を解決します。app/Controller/AppController.phpに次のコード行を追加してください:

public $components = array('Flash');
于 2016-03-10T06:41:19.193 に答える
0

Flash コンポーネントを追加するだけで機能します。

class AppController extends Controller {

     public $components = array('DebugKit.Toolbar','Flash');

}
于 2015-10-22T15:22:24.013 に答える