私はcakePHPにかなり慣れておらず、cakeサイトのすべてのチュートリアルを読んだことがあります。ケーキ2.1を使用して小さなサンプルアプリを作成していますが、問題が発生しました。基本的に、管理者ユーザーは、ログイン時に通常のユーザーがリダイレクトされるページとは異なるページにリダイレクトされるようにします。これを行う簡単な方法があると確信していますが、私は苦労しています!
管理ルーティングを有効にして、認証コンポーネント(およびACLコンポーネントを使用していますが、問題には影響していません)を使用しています。私は2つのログインを持っています。1つはadmin用です-admin_login()と通常のユーザー用のログイン-login()-通常の非管理者ログインは正常に機能します。
私のAppController.phpにはこれがあります:
class AppController extends Controller {
public $components = array(
'Acl',
'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
)
),
'Session'
);
public $helpers = array('Html', 'Form', 'Session');
public function beforeFilter() {
//Configure AuthComponent
$this->Auth->allow('display'); // Allows access to the homepage of the app
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'posts', 'action' => 'add');
}
}
ご覧のとおり、デフォルトでは、ユーザーはログイン時にpostsコントローラーのadd()関数にリダイレクトされます。これは正常に機能します。ただし、admin_login()関数に別のログインリダイレクトを設定するにはどうすればよいですか?
私はここで多くの投稿を読みましたが、ほとんどはケーキ2ではなく古いバージョンのケーキに関連しています。
それが役に立ったら、これはUsersController.phpの私のadmin_login関数です
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('logout');
}
public function admin_login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
if ($this->Session->read('Auth.User')) {
$this->Session->setFlash('You are logged in!');
//$this->redirect($this->Auth->redirect());
// This doesnt work! Grrrrrr
//$this->redirect(array('controller'=>'pages', 'action'=>'admin_index'));
// This nearly works...
if($this->Auth->user())$this->redirect(array('controller' => 'pages', 'action' => 'admin_index'));
}
}
else {
$this->Session->setFlash('Your username or password was incorrect.');
}
}
誰かが私を正しい方向に向けることができますか?