CakePHP $this->ChildController 中的数据为空,AppController 中的数据不为空
我正在运行 CakePHP 1.3.8 stable,但我遇到表单数据问题。当我提交表单时,当我在 AppController 中调试时,我能够看到 $this->data 中包含的数据,但在操作所在的子控制器中它是空的。
这是我的 AppController:
class AppController extends Controller {
var $components = array('Auth', 'Session');
function beforeFilter() {
// Data is not empty here
$this->Auth->authenticate = ClassRegistry::init('User');
$this->Auth->allow('register', 'index');
parent::beforeFilter();
}
}
这是我的 UsersController:
class UsersController extends AppController {
var $name = 'Users';
function register() {
// Data empty at this point
if(!empty($this->data)) {
if($this->data['User']['password'] == $this->Auth->password($this->data['User']['password_confirm'])) {
$this->User->create();
$defaultRole = $this->User->Role->getDefaultRole();
$this->data['User']['role_id'] = $defaultRole['id'];
if($this->User->save($this->data)) {
$this->redirect(array('action' => 'index'));
}
}
}
}
}
表单正在发布到 /user/register
提前致谢!
I am running CakePHP 1.3.8 stable and I have a problem with form data. When I submit my form I am able to see the data contained in $this->data
when I debug in the AppController, but it is empty in my child controller where the action is.
Here is my AppController:
class AppController extends Controller {
var $components = array('Auth', 'Session');
function beforeFilter() {
// Data is not empty here
$this->Auth->authenticate = ClassRegistry::init('User');
$this->Auth->allow('register', 'index');
parent::beforeFilter();
}
}
Here is my UsersController:
class UsersController extends AppController {
var $name = 'Users';
function register() {
// Data empty at this point
if(!empty($this->data)) {
if($this->data['User']['password'] == $this->Auth->password($this->data['User']['password_confirm'])) {
$this->User->create();
$defaultRole = $this->User->Role->getDefaultRole();
$this->data['User']['role_id'] = $defaultRole['id'];
if($this->User->save($this->data)) {
$this->redirect(array('action' => 'index'));
}
}
}
}
}
The form is posting to /users/register
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我明白问题出在哪里了。我在我的 Users 模型中使用自定义哈希方法,并且在 hashPasswords 函数中我没有返回 $data 对象。因此,$data 就被 hashPasswords 函数吞没了,再也看不见了。
感谢您的浏览,如果您犯了类似的错误,我希望这对您有所帮助!
I figured out what the problem was. I am using a custom hashing method in my Users model and within the hashPasswords function I was not returning the $data object. Thus, $data just got swallowed up by the hashPasswords function never to be seen again.
Thanks for looking and I hope this helps you if you have made a similar mistake!
还要查找视图或元素中未终止的表单标签。
Also look for unterminated form tags in the views or elements.