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

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.4k
Accepted
answer

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



11.9k

Thanks man, it works :)