How get Zend_Controller_Plugin_Abstract redirect worked with full url change?it's making Zend_layout disable layout fail

StackOverflow https://stackoverflow.com/questions/4724783

Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top