Christopher Francisco
Christopher Francisco

Reputation: 16278

CakePHP navigation bar logic

The navigation bar in my application will contain User information (name, profile pic), and some associated data. Since the navigation bar is going to be present in the whole application, I'm putting my logic inside AppController::beforeRender() in order to pass data to the View (in this case, the Element nav_bar).

  1. Is this the right place to implement the logic to retrieve user information and associated data?.
  2. Which of the followings is the correct way to load User model data.

    public function beforeRender() {
        $this->loadModel('User');  // this one
        $this->User->find(...                    
    
        ClassRegistry::init('User')->find(... // Or this one?
    

Upvotes: 0

Views: 346

Answers (2)

beta-developper
beta-developper

Reputation: 1774

In your view, Use the AuthComponent class to access to the user information, for example :

to havethe id of user : $use_id = AuthComponent::user('id');

to have all the informations of user : $user = AuthComponent::user();

Upvotes: -1

Guillermo Mansilla
Guillermo Mansilla

Reputation: 3889

I would save the user information in a Session, otherwise you will be hitting the database in every single request.

So, what you wanna do is: in your login() function you get the user information and save it in a Session. Then, in your element, you simply echo the content you want from your Session.

Upvotes: 2

Related Questions