找到你要的答案

Q:How to get attempted log-in user name?

Q:如何尝试登录用户名?

I am using cakephp v3.x

Below is my login function.

public function login()
{
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Flash->error(__('Invalid username or password, try again'));
    }
} //public function login()

I am trying to get the username who tried to log in. I tried reading http://book.cakephp.org/3.0/en/controllers/components/authentication.html I am not able to find the API to retrieve the username. How can this be done?

我使用CakePHP V3 X。

下面是我的登录功能。

public function login()
{
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Flash->error(__('Invalid username or password, try again'));
    }
} //public function login()

I am trying to get the username who tried to log in. I tried reading http://book.cakephp.org/3.0/en/controllers/components/authentication.html I am not able to find the API to retrieve the username. How can this be done?

answer1: 回答1:

You will get all details for user :

$user_details = $this->Auth->User();
echo $user_details['username'];

你会得到用户的所有细节:

$user_details = $this->Auth->User();
echo $user_details['username'];
answer2: 回答2:

Thanks to hint from JimL, the answer is as follows;

$username = $this->request->data['username'];

由于jiml暗示,答案如下;

$username = $this->request->data['username'];
php  cakephp  cakephp-3.0