Reputation: 1107
I am trying to pass the id of the person whosoever logs-in to my site. I am using Auth for login in cakephp. So can anyone tell me how to pass the value during the login and retrieve it at the home page? I am new to cakephp.
This is what I have tried.
app controller.php
class AppController extends Controller {
//...
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'members', 'action' => 'home',$member['Member']['id']),
'logoutRedirect' => array('controller' => 'members', 'action' => 'index')
)
);
MembersController.php
public function home($id = null) {
$this->Member->id = $id;
}
home.ctp
<div align='center'><h2>Hi user welcome to home page </h2></div>
<?php
//I am just trying to print the id of the logged in person
echo $this->Form->value('User.id');
?>
<?php
echo $this->Html->link('LogOut',array('controller'=>'users','action'=>'logout'));
?>
Upvotes: 2
Views: 560
Reputation: 2693
echo $this->Form->value('User.id');
Is part of the FormHelper.
If you would like to have your form prefilled, make use of the conventions. Simply create an input field like this:
echo $this->Form->input('User.id');
If the $this->request->data
contains this value ($this->request->data['User']['id']
) it will automatically fill your form.
In order to retrieve values from the AuthComponent You can simply do something like $userName= $this->Auth->user('name');
(Just make sure the column 'name' exists in your 'users` database).
In order to pass variables to your view, you can simply use the method set which is defined in the Controller
, which is extended by the AppController
which is extend by FooBarsController
.
If you want to set this variable to your view (as mentioned earlier) use set
.
$this->set('userName', $userName);
In your view you can now do:
printf('Welcome %s', $userName);
Upvotes: 3