我對我的項目做了同樣的事情,這就是我所做的。
基本上,我創建了一個新的loginElement.ctp
並將它放在element
文件夾中。我創建了一組新的HTML代碼,這些代碼將適合我希望使用此元素的佈局,然後loginElement.ctp
<form
然後將數據提交到login action
中的users_controller
。
如果你需要,當我以後回家時,我可以在這裏發佈我的確切代碼。
====================================編輯========= ================================
這是我使用的代碼:
首先,您會注意到登錄表單中的action
指向/login
。
我在/config/routes.php
文件設置爲這樣
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
那麼其他的代碼如下所示
/views/elements/thinlogin.ctp
<div id="login">
<form method="post" action="/login" accept-charset="utf-8" class="formBox">
<fieldset>
<input type="hidden" name="_method" value="POST" />
<div class="form-col">
<label for="username" class="lab">Username/Email</label>
<input name="data[User][username]" type="text" id="UserUsername" class="input">
</div>
<div class="form-col form-col-right">
<label for="password" class="lab">Password</label>
<input type="password" name="data[User][password]" id="UserPassword" class="input">
</div>
<div class="form-col form-col-submit">
<input name="" value="Login" class="submit" type="submit">
</div>
<div class="form-col form-col-check">
<label><input name="remember" class="checkbox" type="checkbox">Remember me on this computer</label>
</div>
</fieldset>
</form>
</div>
/views/pages/home.ctp
<div id="home_top_right_top">
<?php
if (!$this->Session->check('Auth.User.id'))
{
echo $this->element('login/thinlogin');
}else{
echo $this->element('login/loggedin');
}
?>
</div>
哇!很好的解釋!我從我的控制器調用該元素,這就是爲什麼我有錯誤消息。所以,我必須從我的觀點調用我的元素。太晚了,我很累,但明天我會測試一下,如果我真的得到你的解釋。非常感謝你! –