4
votes

I have a controller called UsersController, is there a way I can output some values from this controller to the cakephp default layout? Here is what I have tried, the default.ctp:

<?php foreach ($users as $user): ?>
    <ul class="nav navbar-nav navbar-right">
        <?php if (!$this->Session->read('Auth.User.id')) : ?>
        <li>
            <?php echo $this->Html->link('Login',array('controller' => 'us 'action' => 'login', 'full_base' =>'true)); ?></li>
        <li>
            <?php echo $this->Html->link('Register', array('controller' => 'users', 'action' => 'add', 'full_base' => true)); ?>
        </li>
        <?php else: ?>
        <li class="dropdown">
            <a href="<?php echo $this->Html->url(array('controller' => 'users', 'action' => 'edit', 'full_base' => true)); ?>" class="dropdown-toggle" data-toggle="dropdown">
                <?php if(empty($user[ 'User'][ 'filename'])){ ?>
                <i class="fa fa-user fa-lg"></i>
                <?php }else{ echo $this->Html->image($user['User']['filename'], array('alt' => $user['User']['username'],'class'=>'img-responsive img-rounded','width'=>'32','height'=>'32','style'=>'display:inline; background:red;')); } ?>
                <?php $users[ 'User'][ 'username']; ?> <span class="caret"></span>
            </a>
            <ul class="dropdown-menu" role="menu">
                <li>
                    <a href="<?php echo $this->Html->url(array('controller' => 'users', 'action' => 'edit', 'full_base' => true)); ?>"> <i class="fa fa-gear fa-lg"></i> Settings</a>
                </li>

                <li>
                    <a href="<?php echo $this->Html->url(array('controller' => 'users', 'action' => 'logout', 'full_base' => true)); ?>"> <i class="fa fa-power-off fa-lg"></i> Logout</a>
                </li>
                <li><a href="#">Something else here</a>
                </li>
                <li class="divider"></li>
                <li><a href="#">Separated link</a>
                </li>
            </ul>
        </li>
        <?php endif; ?>
    </ul>
<?php endforeach; ?>

And here is my AppController.php:

public function beforeFilter(){
    return $this->getuser();

}   
public function getuser($id = null){
    $this->loadModel('User');
    $user = $this->User->find('all', array(
            'conditions' => array('User.id' => $id)
    ));
$this->set('users',$user);
}

The problem with this code is that,it keeps returning the following notice,

Notice (8): Undefined index: User [APP\View\Layouts\default.ctp, line 71]

please how do i resolve this issue?

2
Where is your UsersController Confirm the $user has proper data or notSupravat Mondal

2 Answers

1
votes

Just change in your controller you have used

public function getuser($id = null){

insted of this use

public function beforeRender($id = null) {

Your AppController.php code should look like this

public function beforeFilter(){
    return $this->getuser();
}

public function beforeRender($id = null){
   $this->loadModel('User');
   $user = $this->User->find('all', array(
           'conditions' => array('User.id' => $id)
  ));
$this->set('users',$user);
}
1
votes

If you want all fields from logged user, you can use this in your view.

$user = $this->Session->read('Auth.User'));

This will return all storage data from users ( username, filename, all fields from user table)