我正在使用cakephp 3.5(今天运行更新)来创建我的第一个非教程Web应用程序.我正在尝试设置我的应用,以便我可以编辑我的用户帐户详细信息(用户名和密码).我不确定我做了什么,但在登录时我实际上无法访问我的edit.ctp(视图).我一直收到"无法找到当前实体的表类"错误.
我最终的目标是让用户能够编辑他们的用户名(这是一个电子邮件地址),并在他们想要的时候更改他们的密码.
有人可以帮助我锻炼我出错的地方,以及为什么我一直得到"无法找到当前实体的表类"错误以及我可以做些什么来解决它.我已经阅读了尽可能多的相关文章.我已经尝试了各种"isAuthorised"函数版本,并且我一直收到同样的错误,所以我确信这是我不知道该找什么的东西.
以下是我的代码:
The User.php file: true, 'id' => false, ]; protected function _setPassword($password) { return (new DefaultPasswordHasher)->hash($password); } } The UsersTable.php file: table('users'); $this->displayField('username'); $this->primaryKey('id'); $this->addBehavior('Timestamp'); $this->hasMany('Agents', [ 'foreignKey' => 'user_id' ]); } /** * Default validation rules. * * @param \Cake\Validation\Validator $validator Validator instance. * @return \Cake\Validation\Validator */ public function validationDefault(Validator $validator) { $validator ->notEmpty('username','Please supply your email address') ->add('username', 'valid', ['rule'=>'email']); $validator ->notEmpty('password','Please supply your password'); $validator ->notEmpty('role','Please select your account type') ->add('role','inList',[ 'rule' => ['inList',['agent','buyer']], 'message' => 'Please select a valid role' ]); $validator ->add('terms_of_service', 'valid', ['rule' => 'boolean']) ->notEmpty('terms_of_service','You must agree to the terms of service to register'); return $validator; } public function isOwnedBy($userId) { return $this->exists(['id' => $userId]); } /** * Returns a rules checker object that will be used for validating * application integrity. * * @param \Cake\ORM\RulesChecker $rules The rules object to be modified. * @return \Cake\ORM\RulesChecker */ public function buildRules(RulesChecker $rules) { $rules->add($rules->isUnique(['username'])); return $rules; } } The relevant pieces UsersController.php file: Auth->allow(['signup','logout']); $this->Auth->deny(['index']); } // The owner of an user can edit and delete it public function isAuthorized($user) { if (in_array($this->request->action, ['edit', 'delete','view'])) { if ($this->Users->isOwnedBy($user['id'])) { return true; } } return parent::isAuthorized($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) { $id = $this->Auth->user('id'); $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(__('Your account has been edited.')); return $this->redirect(['controller','Users','action' => 'edit']); } else { $this->Flash->error(__('The user could not be saved. Please, try again.')); } } $this->set(compact('user')); $this->set('_serialize', ['user']); } The AppController.php file loadComponent('Security');` * * @return void */ public function initialize() { parent::initialize(); $this->loadComponent('RequestHandler'); $this->loadComponent('Flash'); $this->loadComponent('Auth', [ /*'authenticate' => [ 'Form' => [ 'fields' => ['username' => 'email', 'password' => 'password'] ] ],*/ 'authorize' => 'Controller', 'loginRedirect' => [ 'controller' => 'Properties', 'action' => 'myproperties' ], 'logoutRedirect' => [ 'controller' => '', 'action' => 'index' ], 'unauthorizedRedirect' => $this->referer(), //'authError' => 'You must be logged in to view that page.', ]); // Allow the display action so our pages controller continues to work $this->Auth->allow(['display']); } public function beforeFilter(Event $event) { $this->Auth->allow(['index', 'view','logout','search']); } /** * Before render callback. * * @param \Cake\Event\Event $event The beforeRender event. * @return void */ public function beforeRender(Event $event) { if (!array_key_exists('_serialize', $this->viewVars) && in_array($this->response->type(), ['application/json', 'application/xml']) ) { $this->set('_serialize', true); } $this -> set('user', $this -> Auth -> user()); } public function isAuthorized($user) { $childClass = get_called_class(); if(method_exists($childClass, '_isAuthorized')) return $childClass::_isAuthorized($user, $this -> request); return static::_isAuthorized($user, $request); } static public function _isAuthorized($user, $request) { // Admin can access every action if (isset($user['role']) && $user['role'] === 'admin') { return true; } // Default deny return false; } }
kh.tab.. 7
问题是在你的AppController
in beforeRender
方法中你有这个..
$this -> set('user', $this -> Auth -> user());
这意味着您将与所有控制器和操作共享当前用户.
并且还要注意,在用户控制器的编辑操作中,您还调用了一个变量,$user
然后通过set
... 将其发送到视图中.
所以这就是问题的根源,这就是为什么如果你通过users
它更改名称的原因!! 似乎$user
发送beforeRender
方法与$user
用户控制器中的编辑操作中发送的内容之间存在冲突
问题是在你的AppController
in beforeRender
方法中你有这个..
$this -> set('user', $this -> Auth -> user());
这意味着您将与所有控制器和操作共享当前用户.
并且还要注意,在用户控制器的编辑操作中,您还调用了一个变量,$user
然后通过set
... 将其发送到视图中.
所以这就是问题的根源,这就是为什么如果你通过users
它更改名称的原因!! 似乎$user
发送beforeRender
方法与$user
用户控制器中的编辑操作中发送的内容之间存在冲突