gpt4 book ai didi

php - CakePHP登录问题

转载 作者:行者123 更新时间:2023-11-29 22:00:39 26 4
gpt4 key购买 nike

stackoverflowers。我刚刚遇到问题,当我给我的cakePHP网站添加登录功能时,可以添加帐户,但用户无法登录系统

应用 Controller :

namespace App\Controller;

use Cake\Controller\Controller;

class AppController extends Controller
{
public function initialize()
{
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
]
]);

// Allow the display action so our pages controller
// continues to work.
$this->Auth->allow(['display']);
}

用户 Controller :

<?php
namespace App\Controller;

use App\Controller\AppController;

/**
* Users Controller
*
* @property \App\Model\Table\UsersTable $Users */
class UsersController extends AppController
{

/**
* Index method
*
* @return void
*/
public function index()
{
$this->set('users', $this->paginate($this->Users));
$this->set('_serialize', ['users']);
}

/**
* View method
*
* @param string|null $id User id.
* @return void
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function view($id = null)
{
$user = $this->Users->get($id, [
'contain' => ['BacklogOrders', 'Orders']
]);
$this->set('user', $user);
$this->set('_serialize', ['user']);
}

/**
* Add method
*
* @return void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}

/**
* Edit method
*
* @param string|null $id User id.
* @return void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function edit($id = null)
{
$user = $this->Users->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}

/**
* Delete method
*
* @param string|null $id User id.
* @return void Redirects to index.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$user = $this->Users->get($id);
if ($this->Users->delete($user)) {
$this->Flash->success(__('The user has been deleted.'));
} else {
$this->Flash->error(__('The user could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
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('Your username or password is incorrect.');
}
}
public function beforeFilter(\Cake\Event\Event $event)
{
$this->Auth->allow(['add']);
}
}

PHPMyadmin 表:3栏:ID电子邮件密码

密码哈希已被使用。

谢谢!

最佳答案

“Auth”组件在 AppController 中配置。需要配置的属性是“authenticate”、“loginAction”、“loginRedirect”和“logoutRedirect”。类似的东西

    'loginRedirect' => [
'controller' => 'Pages',
'action' => 'display',
'pass[0]' => 'user_home',
],
'logoutRedirect' => [
'controller' => 'Users',
'action' => 'login',
],

“loginRedirect”是用户登录时将登陆的页面。“logoutRedirect”是用户注销后登陆的页面。

关于php - CakePHP登录问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32692608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com