CakePHP $this->ChildController 中的数据为空,AppController 中的数据不为空

发布于 2024-11-01 11:21:27 字数 1476 浏览 1 评论 0原文

我正在运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水染的天色ゝ 2024-11-08 11:21:27

我明白问题出在哪里了。我在我的 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!

如果没结果 2024-11-08 11:21:27

还要查找视图或元素中未终止的表单标签。

Also look for unterminated form tags in the views or elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文