簡體   English   中英

CakePHP從不同的觀點登錄

[英]CakePHP login from different views

我正在使用CakePHP 3.6,並且希望允許用戶從不同的視圖登錄。 我有2個相同的表格,但是每頁上都有一個,而另一頁只有一頁。

讓我告訴你:

您可以在每個頁面上看到此迷你表單

您可以在每個頁面上看到此迷你表單

您可以看到此頁面具有兩種相同的形式。

這是我的AppControler初始化函數

public function initialize()
{
    parent::initialize();

    $this->loadComponent('RequestHandler', [
        'enableBeforeRedirect' => false,
    ]);
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'authorize'=> 'Controller',
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'email',
                    'password' => 'password'
                ]
            ]
        ],
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login'
        ],
         // If unauthorized, return them to page they were just on
        'unauthorizedRedirect' => $this->referer()
    ]);

}

這是我的UsersController登錄功能:

public function login()
{

    if($this->request->is('post')) {
        $user = $this->Auth->identify();
        if($user) {
            $this->Auth->setUser($user);
            $this->Flash->success('You logged succesfully!');
            return $this->redirect($this->referer());
        }

        // Invalid login
        $this->Flash->error('Incorrect login');
    }
}

表單如下(請記住它們是相同的):

<?php if(!$loggedIn): ?>
    <?= $this->Form->create(null, ['class' => 'form-signin']) ?>

        <h2 class="form-signin-heading">Login</h2>
        <?= $this->Form->input('email', ['required' => true, 'class' => 'form-control', 'placeholder' => 'Email', 'label' => false]) ?>
        <?= $this->Form->input('password', ['type' => 'password', 'required' => true, 'class' => 'form-control', 'placeholder' => 'Contraseña', 'label' => false]) ?>
        <?= $this->Form->input('remember', ['type' => 'checkbox', 'value' => 'remember-me']) ?>
        <?= $this->Form->submit('Entrar', ['class' => 'btn btn-lg btn-primary btn-block' ]); ?>

    <?= $this->Form->end() ?>
<?php endif; ?>

這個問題對我來說似乎很明顯,但是我不知道如何解決。 該表單在所有視圖中均不起作用,僅在用戶視圖中有效。 似乎登錄操作僅在我在“用戶”視圖中時有效。

謝謝!

只需以每個視圖中的形式更改為url,以便將POST數據發送到用戶/登錄

echo $this->Form->create(null, [
    'class' => 'form-signin', 
    'url' => ['controller' => 'Users', 'action' => 'login']
]); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM