How to call controllers from cakephp default layout?

I have a controller called UsersController

, is there a way to pass some values ​​from this controller to cakephp's default layout? Here's what I've tried 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 notification:

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

please how to solve this problem?

+3


source to share


2 answers


If you want all fields to be user logged you can use that in your view.

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

      



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

+1


source


Just change your controller that you 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);
}

      

0


source







All Articles