Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

initialize function not working as expected.

Hello I had following structure

class app_base extends Phalcon\Mvc\Controller
{
    public $__currentPage = 1;
    public $__request;

    public function initialize()
    {
        $this->__currentPage = $this->request->getQuery('page','int',1);
    }
}

class app_stage_base extends app_base
{
    public $stageUser = false;

    public function beforeExecuteRoute()
    {
        if(!$this->stageUser) {
            header("login");
        }

    }
    public function initialize()
    {
    }
}

class app_stage_module_model_base extends app_stage_base
{
    public function initialize()
    {
        parent::initialize();
        if(!$this->stageUser) {
            $this->response->redirect("login");
        }
    }
}

class OnlineController extends app_stage_module_model_base
{
    public function initialize()
    {
        parent::initialize();
    }

    public function indexAction()
    {
        $srv = new service_vmodel_online();
        $srv->initRooster();
        $srv->setRoom();
    }
}

$srv->setRoom(); needs session variable. If does not found throws a exception as usual.

How ever. when session expires.

    public function initialize()
    {
        parent::initialize();
        if(!$this->stageUser) {
            $this->response->redirect("login");
        }
    }

Was not working. Code executes to end and throws an exception.

Could anyone lead me to correct this problem.

My Best regards.



6.3k
Accepted
answer

You need to send the response $this->response->redirect("login")->send() && exit;



5.4k

Thanks man, it works :)