cwhisperer
cwhisperer

Reputation: 1926

ZF strange layout behavior

I have a strange behavior with ZF that I can't resolve. I have a layout.phtml and a login.phtml. So when the user is logged in the layout.phtml should be displayed else the login.phtml. This also works, but before displaying the login.phtml, ZF go through layout.phtml and I can confirm this due to errors in the error.log file.

Here what I have in the bootstrap:

public static function _initAcl()
{
    $auth = Zend_Auth::getInstance();
    $acl = new BM_Acl($auth);

    $front = Zend_Controller_Front::getInstance();
    $front->registerPlugin(
        new BM_Controller_Plugin_Acl($auth, $acl)
    );
}

Here what I have in the auth controller:

    public function indexAction() {


    $form = new BM_Form_Login();
    $request = $this->getRequest();

    if ($request->isPost()) {
        if ($form->isValid($request->getPost())) {
            if ($this->_process($form->getValues())) {
                // We're authenticated! Redirect to the home page
                //json validation on login page
                $var = json_encode(array('valid' => true, 'redirect' => 'index'));
                echo $var;
                exit();
            } else {
                $var = json_encode(array('valid' => FALSE, 'error' => 'Authentication failed!', 'redirect' => 'auth'));
                echo $var;
                exit();
            }
        }
    }// end if is POST
    $this->_helper->layout()->setLayout('login'); // special login page
    $this->view->form = $form;
}

Any help will be appreciated... Regards Andrea

P.S. This only happens when I start the application from a new browser window. If I refersh the login page, the layout is not called anymore...

Upvotes: 0

Views: 144

Answers (3)

nwalke
nwalke

Reputation: 3209

Views are for single pages. If you have a singular page you wish to display with a template like layout.phtml, you would edit the index.phtml inside the index action's views directory. If you want to disable the main layout

$this->_helper->layout()->disableLayout();

Or use a blank layout

$this->_helper->layout()->setLayout('blank');

Any code specific to a singular page should be done with a view.

Upvotes: 1

dbrumann
dbrumann

Reputation: 17166

When I understand right, you have a layout that you use on all pages, except for login. Instead of using the view for login as a layout, you should disable layout for this action and just render the login.phtml normally. You can do this by calling the following in your controller's loginAction, instead of setLayout('login'):

$this->_helper->layout()->disableLayout();

This will just disable the layout, but the view is rendered normally.

If you want to do it your way, you have to place the login.phtml into the layout-path, not in the view-path (if you want a more detailed explanation, just ask in a comment).

Upvotes: 0

homelessDevOps
homelessDevOps

Reputation: 20726

Iam not sure, but the i think the layout is redered before the view scripts. You could try to put you logic inside the preDispatch Hook in your Controller.

public function preDispatch() {


    $form = new BM_Form_Login();
    $request = $this->getRequest();

    if ($request->isPost()) {
        if ($form->isValid($request->getPost())) {
            if ($this->_process($form->getValues())) {
                // We're authenticated! Redirect to the home page
                //json validation on login page
                $var = json_encode(array('valid' => true, 'redirect' => 'index'));
                echo $var;
                exit();
            } else {
                $var = json_encode(array('valid' => FALSE, 'error' => 'Authentication failed!', 'redirect' => 'auth'));
                echo $var;
                exit();
            }
        }
    }// end if is POST
    $this->_helper->layout()->setLayout('login'); // special login page
    $this->view->form = $form;
}

Or use an ControllerPlugin:

Zend Controller Plugin - Doc

Upvotes: 0

Related Questions