ktulu05 Posted March 7, 2009 Share Posted March 7, 2009 Hi, I'm new to CakePHP and have been searching for best practices in how to modify layouts dynamically. Basically, I want to change out some menu items in my layout if a user has logged in. I have implemented support for AuthComponent using the cakephp DOC's (user & groups) so my setup should be fairly standard. I want to change the menu's for anonymous users, members, and administrators. I have seen an example like this: function afterFilter () { if ( $isUserLoggedIn ) { $this->layout = 'logged_in_layout'; } else { $this->layout = 'some_other_layout'; } } but I really don't want to have 3 separate layouts. It looks like I could carve up my layout into several elements, and piece them together. It seems like a better option would be just to move the menu layout into 3 separate elements, one for each group. However, I have had no luck figuring out how in the view layer you would be able to tell which, if any, group the logged in user belongs to. After logging into my site, I did a dump of the session object, expecting that AuthComponent would have written something into the session to indicate who was logged in. However, using the pr() function, I don't see this information. Here's some pseudocode of what I would like to do, just not sure how: Default.ctp: if($session->user()->group() == 'Administrator') { echo $this->element('admin/menu'); } else if($session->user()->group() == 'Member') { echo $this->element('member/menu'); } else { echo $this->element('anon/menu'); } I would also like to hide the login form when a user is logged in, but the same general solution should work. I'm using a 'home' controller for the home page, not static text, if that matters. I also tried something like this in the home controller for the index view: class HomeController extends AppController { var $name = 'Home'; var $helpers = array('Html', 'Form', 'Javascript'); var $uses = array(); function index() { $user = $this->Auth->user(); $this->set(compact('user')); } } While I can access the user object, it seems like something that I would think is built into the framework in a more general way. Any help would be appreciated.. -Kevin Quote Link to comment https://forums.phpfreaks.com/topic/148396-authcomponentsessions-and-changing-layouts/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.