这里是CakePHP的新用户。我尚未找到解决此问题的方法。
我想将登录表单作为元素嵌入到现有页面中。我正在使用CakeDC用户插件。目的是将登录表单嵌入页面中,然后在他们登录时更新页面上的某些内容而不刷新页面。在我们开始担心AJAX和SecurityComponent之前,我至少希望将表单作为元素嵌入到其他页面中。
对于初学者,我尝试直接将“/Views/Users/login.ctp”的内容复制并粘贴到元素中。这给了我“ undefined variable “$ Model””的警告,所以我将模型硬编码为“Users.User”(因为我假设访问插件内部模型的符号是“plugin.model”,就像他们在失败),并显示错误消息错误:找不到Users.usersController。
因此,我将表单调整为以“用户”作为模型,而不是$ model,请求现在变得空洞了。如果禁用安全组件,则可以成功登录。这不是一个令人满意的解决方案(我的意思是,我可以选择不验证表单或将“登录”定义为未锁定的操作,但是如果这样做,那么我可能还没有启用登录安全性,对吗? )。
这是app/Plugin/Users/View/Elements/login.ctp的代码:
<div class="users index">
<h2><?php echo __d('users', 'Login'); ?></h2>
<fieldset>
<?php
echo $this->Form->create('User', array(
'action' => 'login',
'id' => 'LoginForm'));
echo $this->Form->input('User.email', array(
'label' => __d('users', 'Email')));
echo $this->Form->input('User.password', array(
'label' => __d('users', 'Password')));
echo '<p>' . __d('users', 'Remember Me') . $this->Form->checkbox('remember_me') . '</p>';
echo '<p>' . $this->Html->link(__d('users', 'I forgot my password'), array('action' => 'reset_password')) . '</p>';
echo $this->Form->end(__d('users', 'Submit'));
?>
</fieldset>
以下是调用该元素的View的代码:
<div id="loginform">
<?php echo $this->element('Users.login'); ?>
</div>
如果我将元素代码复制并粘贴到View中(例如
apps/Plugin/Users/View/Users/login.ctp
中),则无论Security处于打开还是关闭状态,该代码都可以工作,因此我怀疑表单或Security调用会乱七八糟。任何想法或帮助将不胜感激。
最佳答案
我认为问题在于元素本质上是愚蠢的 View -并不是要了解模型。您必须自己传递数据。
我通常在设置auth后将$this->set("authUser", $this->Auth->user());
放入我的AppController中,这使我可以在任何地方检查当前登录用户的数据,包括元素。
您也可以像这样直接传递数据:<?php echo $this->element("Users.login", array("user"=>$user));?>
另外,我在此处的措辞也不明确,但登录应该是未锁定的操作,否则用户将如何首先登录?
关于forms - CakePHP 2.x : Embedding a login form as an element using CakeDC's User plugin and Security component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12922111/