Saturday, June 22, 2013

Naviagation in a CakePHP application

I am new in CakePHP, trying to make an application. In this application, I have a navigation bar, whose Menus I want to be changed according to login status of a user. I think I can't explain clearly, please have a look at this code snippet :

This is my "header" part's(an "element") navigation bar:

           $list="";
           if($authUser)
           {
           $list=array(
                    $this->Html->link('Home',array('controller'=>'pages','action'=>'index')),
                    $this->Html->link('About',array('controller'=>'pages','action'=>'about')),array(
                                                                                                 $this->Html->tag('span',null,array('class'=>'top')),
                                                                                                 $this->Html->tag('span',null,array('class'=>'bottom')),
                                                                                                 $this->Html->link('Sub Menu 1',array('controller'=>'','action'=>'')),
                                                                                                 $this->Html->link('Sub Menu 2',array('controller'=>'','action'=>'')),
                                                                                                 $this->Html->link('Sub Menu 3',array('controller'=>'','action'=>'')),
                                                                                              ),
                    $this->Html->link('Gallery',array('controller'=>'pages','action'=>'gallery')),array(
                                                                                                     $this->Html->tag('span',null,array('class'=>'top')),
                                                                                                     $this->Html->tag('span',null,array('class'=>'bottom')),
                                                                                                     $this->Html->link('Sub Menu 1',array('controller'=>'','action'=>'')),
                                                                                                     $this->Html->link('Sub Menu 2',array('controller'=>'','action'=>'')),
                                                                                                     $this->Html->link('Sub Menu 3',array('controller'=>'','action'=>'')),
                                                                                                  ),
                    $this->Html->link('My Posts',array('controller'=>'pages','action'=>'myPosts/1')),
                    $this->Html->link('Blog',array('controller'=>'pages','action'=>'blog')),
                    $this->Html->link('Contact',array('controller'=>'pages','action'=>'contact')),
                    $this->Html->link('Logout',array('controller'=>'users','action'=>'logout'))
                 );
           }
           else
           {
           $list=array(
                    $this->Html->link('Home',array('controller'=>'pages','action'=>'index')),
                    $this->Html->link('About',array('controller'=>'pages','action'=>'about')),array(
                                                                                                 $this->Html->tag('span',null,array('class'=>'top')),
                                                                                                 $this->Html->tag('span',null,array('class'=>'bottom')),
                                                                                                 $this->Html->link('Sub Menu 1',array('controller'=>'','action'=>'')),
                                                                                                 $this->Html->link('Sub Menu 2',array('controller'=>'','action'=>'')),
                                                                                                 $this->Html->link('Sub Menu 3',array('controller'=>'','action'=>'')),
                                                                                              ),
                    $this->Html->link('Gallery',array('controller'=>'pages','action'=>'gallery')),array(
                                                                                                     $this->Html->tag('span',null,array('class'=>'top')),
                                                                                                     $this->Html->tag('span',null,array('class'=>'bottom')),
                                                                                                     $this->Html->link('Sub Menu 1',array('controller'=>'','action'=>'')),
                                                                                                     $this->Html->link('Sub Menu 2',array('controller'=>'','action'=>'')),
                                                                                                     $this->Html->link('Sub Menu 3',array('controller'=>'','action'=>'')),
                                                                                                  ),
                    $this->Html->link('Blog',array('controller'=>'pages','action'=>'blog')),
                    $this->Html->link('Contact',array('controller'=>'pages','action'=>'contact'))
                 );
                 }
           echo $this->Html->nestedList($list);

what I want, if a user is logged in "My Post" and "Logout" menus will be there, otherwise not. I am getting the value of the $authUser variable from all of the "View" files, when I set the "header" there. This is the code that I am keeping in all "View" files :

           echo $this->element('header',array("authUser"=>$this->Auth->loggedIn()),array("cache"=>"long_view"));
This creates 2 errors, saying "There is no Auth helper in the library" and "$authUser" could not be recognized, in the "header.ctp". What should I do now ?

By the way, I am using "AuthComponent" for user Login, Logout and SignUp. Should I use Session instead of Authentication, or mix of these 2, like when a user gets logged in, a new session value will be created and that will be checked whenever it needs to do that ?

I got some help from another developer, he gave me that "authUser" idea, I think it looks good, but that "Auth" helper creates problem.

Thanks

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: