1

I found this helper code from rob allens' Zend_Auth login/logout tutorial

class Zend_View_Helper_LoggedInAs extends Zend_View_Helper_Abstract

    {
        public function loggedInAs()
        {
            $auth = Zend_Auth::getInstance();
            if ($auth->hasIdentity()) {
                $username = $auth->getIdentity()->WSLoginName;
                $logoutUrl = $this->view->url(array('controller' => 'login',
                'action' => 'logout', 'module' => 'member'), null, true);
                return 'Welcome '. $username . '. <a href="'. $logoutUrl . '">Logout</a>';
            }

            $request = Zend_Controller_Front::getInstance()->getRequest();
            $controller = $request->getControllerName();
            $module = $request->getModuleName();
            $action = $request->getActionName();
            if($controller == 'login' && $action == 'index'){
                return '';
            }

            $loginUrl = $this->view->url(array('controller' => 'login', 'action' => 'index'));
            return '<a href="'. $loginUrl . '">Login</a>';
        }
    }

now my question is, how am i gonna use this helper in a different controller, within the same module ?, because apparently, in the said tutorial, this helper is used in a layout file , and then the user gets redirected to the indexController. when user logs out, it gets redirected to the login page again.. my problem is this, I added a new Controller within the same module where the LoginController controller and the said helper resides, and this new controller is using the same layout file where that helper is being called, when I clicked the logout link, it doesn't work anymore

Charles
  • 50,943
  • 13
  • 104
  • 142
sasori
  • 5,249
  • 16
  • 86
  • 138
  • check the $logoutUrl and modify it to your controller name.. – opHASnoNAME Nov 02 '10 at 18:10
  • yeah I edited the $logoutUrl, but this time, when am on the IndexController view page of the same module, the logout link does work, but it works for the other controller that i created, so how will i make this helper work for all the controllers in the same module ? – sasori Nov 03 '10 at 03:05
  • 2
    @sasori This is a view helper, you'll use in your view, whenever it's a layout or action view script: `echo $this->loggedInAs();` So, what do you mean with `use this helper in a different controller` ? – Keyne Viana Nov 03 '10 at 13:39
  • 1
    agre with keyne, its a view helper script, whatever you change your controller should not affect the result. btw what do you mean with "logout link doens't work anymore" ? – Jeg Bagus Nov 03 '10 at 17:44
  • member.phtml #this is where the $this->loggedInAs() helper is being called Member_LoginController #this is where authentication happens, Member_IndexController #if user exists in db,it gets redirected here. Member_DetailsController = this is the new controller I added. LoggedInAs.php = this is the helper.index.phtml = the default view after login,and logout works.. details/index.phtml #this is where the logout doesn't work – sasori Nov 04 '10 at 03:52
  • this one works = resources.router.routes.member-login.route = /member/login resources.router.routes.member-login.defaults.module = member resources.router.routes.member-login.defaults.controller = login resources.router.routes.member-login.defaults.action = index – sasori Nov 04 '10 at 03:56
  • this one is the route where the log out doesn't work resources.router.routes.member-details.route = /member/details resources.router.routes.member-details.defaults.module = member resources.router.routes.member-details.defaults.controller = details resources.router.routes.member-details.defaults.action = index – sasori Nov 04 '10 at 03:57
  • 1
    It's a bad idea to namespace your code with Zend, see my response to http://stackoverflow.com/questions/2335545/how-to-add-a-view-helper-directory-zend-framework/2338082#2338082 – David Snabel-Caunt Nov 05 '10 at 11:23

2 Answers2

2

To make this work across different modules, you will have to register it as a "global" helper. To do that, add the following somewhere in your bootstrap file.

//Bootstrapping file..

//Initialize and/or retrieve a ViewRenderer object on demand via the helper broker
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->initView();

//add the global helper directory path
$viewRenderer->view->addHelperPath('/your/path/to/GlobalViewHelpers','My_View_Helper');

Particularly, I like to set up the following:

'/your/path/to/GlobalViewHelpers' as APPLICATION_PATH."/../library/CompanyName/View/Helper"

and

'My_View_Helper' as 'CompanyName_View_Helper'

After that, take the code that Mr. Rob Allen created and place it in /your/path/to/GlobalViewHelpers

Rename the class to be 'My_View_Helper_LoggedInAs'

You should the be able to have the following:

/application/layout/main.phtml

...    
<body>
        <div id='profile-panel'>
            <?=$this->loggedInAs();?>
        </div>
        <?
            $flashMessenger = Zend_Controller_Action_HelperBroker::getHelper('flashMessenger');
            $this->messages = $flashMessenger->getMessages();
        ?>
...

Additionally, You will have to change a few lines of code to meet your needs as far as places where your login and logout live.

<?php
class Zend_View_Helper_LoggedInAs extends Zend_View_Helper_Abstract 
{
    public function loggedInAs ()
    {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $username = $auth->getIdentity()->username;
            //CHANGE HERE: This should be your Logout page
            $logoutUrl = $this->view->url(array('controller'=>'auth',
                'action'=>'logout',
                'module'=>'default'), null, true);
            return 'Welcome ' . $username .  '. <a href="'.$logoutUrl.'">Logout</a>';
        } 

        $request = Zend_Controller_Front::getInstance()->getRequest();
        $controller = $request->getControllerName();
        $action = $request->getActionName();
        //CHANGE HERE: This should be your login page
        if($controller == 'auth' && $action == 'index') {
            return '';
        }
        //CHANGE HERE: This is also your login page.
        $loginUrl = $this->view->url(array(
            'module'=>'default',
            'controller'=>'auth', 
            'action'=>'index'));
        return '<a href="'.$loginUrl.'">Login</a>';
    }
}
?>

Hope this helps.

Sources:

http://akrabat.com/zend-auth-tutorial/

http://www.mixedwaves.com/2010/03/accessing-and-using-zend-view-helpers-from-a-common-directory/

General Redneck
  • 1,240
  • 2
  • 13
  • 28
  • 1
    Why make so complex, when its so easy? see: http://stackoverflow.com/questions/3741556/how-i-add-view-helper-in-zend/7907954#7907954 –  Oct 26 '11 at 19:46
  • 1
    @JavaNotFred, Your reply to the other question works wonderfully if you don't have several modules you want to use a view helper for. If the application only has the single module (default), you have the views/helpers folder available to everything. Good Call though, it CAN be simpler. – General Redneck Oct 26 '11 at 23:07
1

Your logout action is in a controller. You must have a route that looks like this: /module/controller/logout/. Use this route in your helper as logout url. Now from whereever you logout you get redirected to the logout action.

chrisweb
  • 1,428
  • 19
  • 25