-1

私たちの CakePHP アプリケーションでは、ログイン用に Auth コンポーネントを試しました。

これは AppController です。

class AppController extends Controller {
    public $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect' => array('controller' => 'homes', 'action' => 'dashboard'),
            'logoutRedirect' => array('controller' => 'pages', 'action' => 'home')
        )
    );

    public function beforeFilter() {
        $this->Auth->allow('index','logout','display','home');
    }

これはユーザーコントローラーです:

class UsersController extends AppController {

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('add');

    }

    public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash(__('Invalid username or password, try again'));
            }
        }
    }

    public function logout() {
        $this->redirect($this->Auth->logout());
    }


    public function index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }


    public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('Checkin now'));
                $this->redirect(array('action' => 'login'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        }
    }
}

次に、次の手順に従います。

  1. ブラウザに入力された URLhttp://localhost/cakephp/
  2. Homepage から、ボタンを使用してログインhome.ctpするためにナビゲートしますhttp://localhost/cakephp/users/loginLogin
  3. ユーザー名とパスワードを入力し、ログインボタンをクリックします
  4. home.ctp次に、 に記載されているページではなく、以前にアクセスしたページにリダイレクトしますAppController

2 回目の試行:

  1. URLhttp://localhost/cakephp/users/login/フィールドから直接アクセス
  2. 次にログイン資格情報を入力すると、 に記載されている正しいページにリダイレクトされAppControllerます。

なぜAuthコンポーネントはこのように振る舞うのか.....

4

2 に答える 2

0

アプリコントローラーを確認してください。ログインをホームからダッシュボードにリダイレクトしていますが、ユーザーからログインにリダイレクトできません

あなたの変更:

アプリ コントローラー

        'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),

およびユーザーコントローラー

   public function login() {

    if ($this->request->is('post')) {

        /* login and redirect to url set in app controller */

        if ($this->Auth->login()) {

            return $this->redirect(array('controller' => 'users','action' => 'dashboard'));

        }

        $this->Session->setFlash(__('Invalid username or password, try again'));

    }

}
于 2013-09-13T05:54:30.217 に答える