We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Change layout and dispatch loop

Hello, I'm not sure about the category, please change it if necessary.

For one of my actions, (IndexController::loginAction), I would like to change the layout script to more basic one than the standard admin layout and it works great with:

public function loginAction()
{
    $this->view->setRenderLevel(View::LEVEL_LAYOUT)->setLayout('login');
}

Well, now I have the Security plugin, which needs to redirect the user to the login page if not logged in and works perfect with this code:

$di['dispatcher'] = function() {
    //.......
    $eventsManager->attach('dispatch', $adminSecurity);
    //.......
    return $dispatcher;
};

But the problem is - it still use the main layout and not my login layout. It seems the setRenderLevel call have no effect anymore. In the Security plugin, I'm using beforeExecuteRoute method and I've tried to change to other dispatch events. I also tried to change the event to which it is attached to dispatch:beforeDispatch with no effect.

Any help is welcome!



3.0k
Accepted
answer

Solved:

$eventsManager->attach('dispatch:afterDispatch', $adminSecurity);


15.1k
edited Apr '14

I think you should do your security checks before dispatch, not after. https://docs.phalcon.io/en/latest/reference/dispatching.html

In your current situation, I can still post any malicious data to your admin, which will successfuly process it and only then check if I'm logged in or not.

Remember that you can use 'View picking' from your controllers to change the view / layout.

        // Pick "views-dir/products/search" as view to render
        $this->view->pick("products/search");

https://docs.phalcon.io/en/latest/reference/views.html#picking-views