Zend 框架中的视图助手 [英] view helper in zend framework

查看:29
本文介绍了Zend 框架中的视图助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 rob allens 的 Zend_Auth 登录/注销教程中找到了这个帮助程序代码

class Zend_View_Helper_LoggedInAs 扩展 Zend_View_Helper_Abstract{公共函数 loginAs(){$auth = Zend_Auth::getInstance();如果 ($auth->hasIdentity()) {$username = $auth->getIdentity()->WSLoginName;$logoutUrl = $this->view->url(array('controller' => 'login','动作' =>'注销', '模块' =>'会员'), null, true);返回欢迎".$用户名.'.<a href="'.$logoutUrl.'">注销</a>';}$request = Zend_Controller_Front::getInstance()->getRequest();$controller = $request->getControllerName();$module = $request->getModuleName();$action = $request->getActionName();if($controller == '登录' && $action == 'index'){返回 '​​';}$loginUrl = $this->view->url(array('controller' => 'login', 'action' => 'index'));return '<a href="'.$loginUrl .'">Login</a>';}}

现在我的问题是,我将如何在同一个模块中的不同控制器中使用这个助手?因为显然,在上述教程中,这个助手用于布局文件,然后用户被重定向到索引控制器.当用户注销时,它再次被重定向到登录页面.正在调用该助手,当我单击注销链接时,它不再起作用

解决方案

要使这项工作在不同的模块中工作,您必须将其注册为全局"帮助程序.为此,请在引导文件中的某处添加以下内容.

//引导文件..//通过辅助代理按需初始化和/或检索 ViewRenderer 对象$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');$viewRenderer->initView();//添加全局助手目录路径$viewRenderer->view->addHelperPath('/your/path/to/GlobalViewHelpers','My_View_Helper');

特别是,我喜欢设置以下内容:

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

'My_View_Helper' 为 'CompanyName_View_Helper'

之后,将 Rob Allen 先生创建的代码放入/your/path/to/GlobalViewHelpers

将类重命名为My_View_Helper_LoggedInAs"

您应该能够拥有以下内容:

/application/layout/main.phtml

<代码>...<身体><div id='profile-panel'><?=$this->loggedInAs();?>

<?$flashMessenger = Zend_Controller_Action_HelperBroker::getHelper('flashMessenger');$this->messages = $flashMessenger->getMessages();?>...

此外,您必须更改几行代码才能满足您登录和注销所在位置的需求.

hasIdentity()) {$username = $auth->getIdentity()->username;//在这里更改:这应该是您的注销页面$logoutUrl = $this->view->url(array('controller'=>'auth','action'='注销','module'=>'default'), null, true);返回欢迎".$用户名.'.<a href="'.$logoutUrl.'">注销</a>';}$request = Zend_Controller_Front::getInstance()->getRequest();$controller = $request->getControllerName();$action = $request->getActionName();//在这里更改:这应该是您的登录页面if($controller == 'auth' && $action == 'index') {返回 '​​';}//在这里更改:这也是您的登录页面.$loginUrl = $this->view->url(array('模块'=>'默认','控制器'=>'身份验证','动作'=>'索引'));return '<a href="'.$loginUrl.'">登录</a>';}}?>

希望这会有所帮助.

来源:

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

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

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/

这篇关于Zend 框架中的视图助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆