简体   繁体   中英

view helper in zend framework

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

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/

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM