简体   繁体   English

CakePHP从不同的观点登录

[英]CakePHP login from different views

I'm using CakePHP 3.6 and I want to allow the user to login from different views. 我正在使用CakePHP 3.6,并且希望允许用户从不同的视图登录。 I have 2 forms that are the same, but one is on every page and the another one is only in 1 page. 我有2个相同的表格,但是每页上都有一个,而另一页只有一页。

Let me show you that: 让我告诉你:

您可以在每个页面上看到此迷你表单

您可以在每个页面上看到此迷你表单

您可以看到此页面具有两种相同的形式。

Here's my initialize function of AppControler 这是我的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()
    ]);

}

Here's my login function of UsersController: 这是我的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');
    }
}

Here's the form (remember they are the same): 表单如下(请记住它们是相同的):

<?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; ?>

The problem seems pretty obvious to me but I don't know how to solve it. 这个问题对我来说似乎很明显,但是我不知道如何解决。 The form that's in all the views it's not working, but only in the Users view. 该表单在所有视图中均不起作用,仅在用户视图中有效。 It seems like the login action it's only working if I'm in the Users view... 似乎登录操作仅在我在“用户”视图中时有效。

Thanks! 谢谢!

simply change to url in the form that's in every view so that the POST data is sent to users/login 只需以每个视图中的形式更改为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