I have been doing research on the group and it is awesome! I am new to
CakePHP and trying to learn as quickly as I can. This is my first post
to this group. Here is what I am trying to accomplish:
1) If user not logged in, have navigation bar show only button for
"Log In" and static start page.
2) When "Log In" requested, go to login.ctp
3) After successful log in, go back to static start page and display
message "You are now logged in." and change the button on the
navigation bar to "Log Out". [Eventually I would like to personalize
the message to be "You are now logged in as {userID}" but I don't know
how to do that yet...]
4) When "Log Out" requested, log the user out and display message
"Logged Out." and change the navigation bar to "Log In" on the static
page.
I am very very close to accomplishing those objectives. I have 1
through 3 working, with the exception of displaying the userID.
When I press "Log Out", the button does change to "Log In" but I am
redirected back to the login.ctp page. I don't want that! It must be
something simple, as usual in CakePHP. Here is my code:
The app_controller.php file
class AppController extends Controller {
var $view = 'Theme';
var $theme = 'CornFlowerBlue';
var $components = array(
'Email',
'Session',
'Cookie',
'Auth'
);
var $helpers = array(
'Javascript',
'Form',
'Html',
'Session'
);
function beforeFilter() {
$this->Auth->loginAction = array
('controller'=>'users','action'=>'login');
$this->Auth->loginRedirect = array
('controller'=>'users','action'=>'add');
$this->Auth->logoutRedirect = '/';
$this->Auth->allow('display');
$this->Auth->authorize = 'controller';
$this->Auth->userScope = array('User.active'=>1);
}
The default.ctp file in /app/view/layouts
<body>
<div id="container">
<div id="header">
<h1><?php echo $html->link('BG Products, Inc. Canada','/');
?></h1>
</div>
<div id="navbar">
<ul>
<?php if (!$session->check('Auth.User.id')): ?>
<li><?php echo $html->link(__
('login',true),''.'/'.'users'.'/'.'login'); ?></li>
<?php else: ?>
<li><?php echo $html->link(__
('logout',true),''.'/'.'users'.'/'.'logout'); ?></li>
<?php endif;?>
</ul>
</div>
<div id="wrapper">
<div id="content">
<?php
if ($session->check('Message.flash')):
$session->flash();
endif;
?>
<?php echo $content_for_layout; ?>
</div>
The users_controller.php file
class UsersController extends AppController {
var $name='Users';
var $uses = array(
'User'
);
function beforeFilter() {
$this->Auth->autoRedirect = false;
}
function login() {
$this->Session->setFlash('Please enter your Username and
Password.');
//-- code inside this function will execute only when
autoRedirect
was set to false
if ($this->Auth->user()) {
$this->Session->setFlash('You are now logged in.');
$this->redirect($this->Auth->redirect());
}
}
function logout() {
$this->Session->setFlash('Logout');
$this->redirect($this->Auth->logout());
}
The login.ctp file is just standard
<?php if($session->check('Message.auth')) $session->flash('auth'); ?>
<?php echo $form->create('User', array('action' => 'login')); ?>
<?php echo $form->input('User.username'); ?>
<?php echo $form->input('User.password'); ?>
<?php echo $form->end('Login'); ?>
The logout.ctp file is present, but empty.
Thanks in advance for your advice!
FD
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---