Zend_Controller_Plugin_Abstract 重定向如何与完整的 url 更改一起工作?它使 Zend_layout 禁用布局失败

发布于 2024-10-12 07:15:59 字数 1330 浏览 7 评论 0原文

我有一个管理 webapp。在执行任何操作之前必须登录。现在 /default/index/index 具有登录表单,它是 ExtJs 组件。基本上登录过程是 ajax 过程。我创建了一个插件来禁用渲染和布局并检查用户是否登录(还没有完整的 acl)。

这是代码:

 public function  preDispatch(Zend_Controller_Request_Abstract $request) {
    parent::preDispatch($request);

    if($request->isXmlHttpRequest()){
        $ViewHelper = Zend_Controller_Action_HelperBroker::getStaticHelper("ViewRenderer");
        $ViewHelper->setNoRender(true);
        Zend_Layout::getMvcInstance()->disableLayout();

    }


    $module = $request->getModuleName();
    $controller = $request->getControllerName();
    $action = $request->getActionName();

    if(!Zend_Auth::getInstance()->hasIdentity()){
        $url = "/".$module."/".$controller."/".$action;
          $session = new Zend_Session_Namespace("myapp.auth");
        $session->requestURL = $url;
        $request->setModuleName("default");
        $request->setControllerName("index");
        $request->setActionName("index");
        $request->setDispatched();

    }
}

这似乎有效,但地址栏仍然有原始的请求网址。 例如,我在网址栏中输入 "myapp/admin/cpanel" ,它会在浏览器上打开登录页面,而地址栏仍然有 "myapp/admin/cpanel"< /code>. 目前登录失败,因为输出有一些 html 渲染,我相信是来自登录页面(直接点击时工作正常)。

有没有人以前经历过这个,或者只是我做错了什么。如果你能分享你的经验,我会很高兴。

感谢您阅读本文。

I have an admin webapp.one have to login before perfoming any action.Now the /default/index/index has the login form which is an ExtJs component.basically the login process is an ajax one.i've created a plugin to disable the rendering and layout and to check whether user is logged in or not(no full acl yet).

here is the code:

 public function  preDispatch(Zend_Controller_Request_Abstract $request) {
    parent::preDispatch($request);

    if($request->isXmlHttpRequest()){
        $ViewHelper = Zend_Controller_Action_HelperBroker::getStaticHelper("ViewRenderer");
        $ViewHelper->setNoRender(true);
        Zend_Layout::getMvcInstance()->disableLayout();

    }


    $module = $request->getModuleName();
    $controller = $request->getControllerName();
    $action = $request->getActionName();

    if(!Zend_Auth::getInstance()->hasIdentity()){
        $url = "/".$module."/".$controller."/".$action;
          $session = new Zend_Session_Namespace("myapp.auth");
        $session->requestURL = $url;
        $request->setModuleName("default");
        $request->setControllerName("index");
        $request->setActionName("index");
        $request->setDispatched();

    }
}

this seems to work but then the address bar still have the original request url.
for example i've typed "myapp/admin/cpanel" in the url bar and it opens the login page on the browser while the address bar still has "myapp/admin/cpanel".at the moment the login fails because the output has some html rendering i believe is from the login page(which has been working fine when hit directly).

Has anyone has experienced this before or it's just me doing something wrong.I'will be glad if you can share your experience with this.

thanks for reading this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦醒时光 2024-10-19 07:15:59

这似乎有效,但地址栏仍然有原始请求
网址。例如,我在网址栏中输入“myapp/admin/cpanel”,它会打开
浏览器上的登录页面,而地址栏仍然有
“myapp/admin/cpanel”。

如果您不喜欢这种行为,您可能需要考虑将用户重定向到登录表单,而不是在每个控制器之前处理登录。如果您的登录页面目前确实可以访问,那么这应该像写作一样简单:

<?php
public function  preDispatch(Zend_Controller_Request_Abstract $request) {
    parent::preDispatch($request);

    if($request->isXmlHttpRequest()){
        $ViewHelper = Zend_Controller_Action_HelperBroker::getStaticHelper("ViewRenderer");
        $ViewHelper->setNoRender(true);
        Zend_Layout::getMvcInstance()->disableLayout();
    }

    $module = strtolower( $request->getModuleName( ) );
    $controller = strtolower( $request->getControllerName( ) );
    $action = strtolower( $request->getActionName( ) );

    /** Check to see if the controller is already the login controller to prevent an endless loop. */
    //corrected if(!array( 'default', 'index', 'index' ) === array()) to if(array( 'default', 'index', 'index' ) !== array())
    if( ( array( 'default', 'index', 'index' ) !== array( $module, $controller, $action ) ) {

        if( !Zend_Auth::getInstance()->hasIdentity( ) ) {
            $url = "/".$module."/".$controller."/".$action;
              $session = new Zend_Session_Namespace("myapp.auth");
            $session->requestURL = $url;
            $this->getResponse()->setRedirect( '/' )->sendResponse( );
        }
    }
}

虽然我无法测试它,但想法应该很清楚。

this seems to work but then the address bar still have the original request
url. for example i've typed "myapp/admin/cpanel" in the url bar and it opens
the login page on the browser while the address bar still has
"myapp/admin/cpanel".

In case you dislike that behaviour, you might want to consider redirecting the user to the login-form, instead of handling the login before each and every controller. If your login page is actually accessible at the moment, this should be as trivial as writing:

<?php
public function  preDispatch(Zend_Controller_Request_Abstract $request) {
    parent::preDispatch($request);

    if($request->isXmlHttpRequest()){
        $ViewHelper = Zend_Controller_Action_HelperBroker::getStaticHelper("ViewRenderer");
        $ViewHelper->setNoRender(true);
        Zend_Layout::getMvcInstance()->disableLayout();
    }

    $module = strtolower( $request->getModuleName( ) );
    $controller = strtolower( $request->getControllerName( ) );
    $action = strtolower( $request->getActionName( ) );

    /** Check to see if the controller is already the login controller to prevent an endless loop. */
    //corrected if(!array( 'default', 'index', 'index' ) === array()) to if(array( 'default', 'index', 'index' ) !== array())
    if( ( array( 'default', 'index', 'index' ) !== array( $module, $controller, $action ) ) {

        if( !Zend_Auth::getInstance()->hasIdentity( ) ) {
            $url = "/".$module."/".$controller."/".$action;
              $session = new Zend_Session_Namespace("myapp.auth");
            $session->requestURL = $url;
            $this->getResponse()->setRedirect( '/' )->sendResponse( );
        }
    }
}

I'm not able to test it though, but the thought should be clear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文