I have a view with a username, password and a submit button. This view is called login.ctp, so the way I had it previous to CakePHP was I had a <div> at the top of the page and in that <div> the login would be visible through out all the pages so basically you can log in from anywhere, I was using AJAX. My issue now is that I do not know how to do it in CakePHP because some of the other views have a <form> tag, and I think it needs a <form> tag for the login too? so they conflict..Also, so two things
-
How can I place that div back at the top of the page and make it work with my login function in the
UsersController? -
How can I do it so that the
<forms>do not conflict?
related code
<?php
class UsersController extends AppController {
var $uses = array("User");
var $components = array('Auth', 'Session');
function index()
{
$this->set('users', $this->User->find('all'));
$this->layout = 'master_layout';
}
function beforeFilter() {
$this->Auth->allow('add');
}
function add() {
if (!empty($this->data)) {
//pass is hashed already
//->data['User']['password'] = $this->Auth->password($this->data['User']['password']);
if ($this->User->save($this->data)) {
$this->Session->setFlash('Your were registered!.');
$this->redirect(array('action' => 'index'));
}
}
$this->layout = 'master_layout';
}
//IF THE DATABASE IS SET UP CORRECTLY CAKE AUTHENTICATES AUTOMATICALLY NO
//LOGIC IS NEEDED FOR LOGIN http://book.cakephp.org/view/1250/Authentication
function login() {
$this->layout = 'master_layout';
$this->data['User']['password'] = '';
}
function logout() {
$this->redirect($this->Auth->logout());
}
}
?>
This is pretty simple. If you want to form to repeat across more than just one page, you should use an element. Create the loginform.ctp file in the app/views/elements folder and just put a
<?php echo $this->element('loginform'); ?>wherever you want it. In your loginform.ctp file, you should have something like this:As long as you specify which controller and action the form is for, Cake will take care of it so that the forms do not conflict.
If that doesn’t work, try just the html: