Monday, December 29, 2008

Element - Login form or Welcome view in a layout

Hi all bakers !

I am struggling to find the right way to include a user index view
that can either display a login form or a welcome message. I dont
really know where to put the logic where to display the correct view.

Here's a bit of my code

in my layout default.ctp:

<?php echo $this->element('users/index'); ?>

my elements/users/index.ctp:

<?php $connected=$this->requestAction('users/isConnected');
if ($connected) {
echo $this->element('users/welcome');
} else {
echo $this->element('users/login');
}
?>

my elements/users/welcome.ctp

<?php $name=$this->requestAction('users/welcome'); ?>
<table>
<tr>
<td>Welcome <?php echo $name; ?></td>
<td><a href="/cake/users/logout">Logout</a></td>
</tr>
</table>

my elements/users/login.ctp

<?php echo $form->create('User',array('action' => 'login')); ?>
<?php
echo $form->input('firstname');
echo $form->input('password');
?>
<?php echo $form->end('Login');?>


and eventually my Controller

class UsersController extends AppController {

var $name = 'Users';
var $helpers = array('Html', 'Form');

function isConnected() {
$user = $this->Session->read('firstname');
if ($user) return true;
else return false;
}

function index() {
}

function login () {
if ($this->data) {
$result = $this->User->findByFirstname($this->data['User']
['firstname']);
if($result && $result['User']['password'] == $this->data['User']
['password']) {
$this->Session->write('firstname',$result['User']
['firstname']);
$this->Session->write('id',$result['User']['id']);
//$this->redirect(array('action'=>'welcome'),null,true);
}
}
}

function logout() {
$this->Session-> delete('firstname');
$this->Session-> delete('id');
//$this->redirect(array('action'=>'login'),null,true);
}

function welcome() {
return $this->Session->read('firstname');
}
}
?>


I am not very happy with the ><a href="/cake/users/logout">Logout</a>
in the welcome.ctp plus I still got some "cannot view" error message
displayed into my page.
I'd like to know the best practice to manage this case and in general
to manage the elements.
I would really prefer using redirect in my Controller but this does
not work in elements....

Thanks very much for your help

Fabien

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: