繁体   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