Jump to content

AuthComponent/Sessions and Changing Layouts


Recommended Posts



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:



if($session->user()->group() == 'Administrator')
echo $this->element('admin/menu'); 
else if($session->user()->group() == 'Member')
echo $this->element('member/menu'); 	
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();



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..



Link to post
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.