view helper in zend framework

你说的曾经没有我的故事 提交于 2019-12-14 02:25:55

问题


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


回答1:


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/




回答2:


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.



来源:https://stackoverflow.com/questions/4078621/view-helper-in-zend-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!