Derek
Derek

Reputation: 859

CakePHP 2.5: setting global variable

I am setting a variable '$loggedIn' in my AppController to be accessed globally to identify logged in users but it only results true if my url is calling the Users controller. for example if you visit the users/index with url it shows that you are logged in. If I visit pages/home it doesn't show logged in. below us the code in appcontroller and my view (default.ctp).

Controllers/AppController.php

public function beforeFilter() {
$this->Auth->allow('index', 'view');
$this->set('loggedIn', $this->Session->read('Auth.User'));//fix here
}

View/Layouts/default.ctp

    <div id="header">
    <div class="top-links">
    <?php if($loggedIn) { //fix here
        echo $this->Html->link('Register', array('controller'=>'users','action'=>'register')); 
        echo '&nbsp;|&nbsp;';
        echo $this->Html->link('Login', array('controller'=>'users','action'=>'login')); 

        } else {
            echo $this->Html->link('My Profile', array('controller'=>'users', 'action' => 'edit', $loggedIn['User']['id']));//fix here
            echo $this->Html->link('Logout', array('controller'=>'users','action'=>'logout'));
        }
    ?>
    </div>
    <a href="/cake"><img src="/img/logo.png" class="top-logo" /></a>
    <?php
        echo $this->element('top_menu');

    ?>

</div>

Upvotes: 2

Views: 191

Answers (1)

Nick Zinger
Nick Zinger

Reputation: 1174

You are probably overwriting the beforeFilter within the PagesController. For the PagesController to keep using the AppController->beforeFilter code, you need this in the PagesController:

function beforeFilter() {
        parent::beforeFilter();
        //rest of the code for this function
}

Upvotes: 2

Related Questions