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 Controller

Hello guys,

I have this in my main controller:

<?php

namespace Tendcom\Controllers;

use Phalcon\Mvc\Controller;
use Tendcom\Library\Acl;
use Tendcom\Library\Menu;
use Tendcom\Models\Users\Users;

class ControllerBase extends Controller {

    public function initialize() {

        // set template
        $this->view->setTemplateAfter('tendcom');
        $this->getAssets();

        // set test active
        $this->view->isTest = ($this->config->isTest) ? true : null;

        // get url
        $baseUri = $this->url->getBaseUri();
        $this->view->baseUri = $baseUri;

        // verify if session exists
        if(!$this->session->get('userTendcom')) {
            header('Location: /login');
            return false;
        }

        // get user profile
        $user = $this->session->get('userTendcom');
        $this->view->user = $user;

and this in another controller:

<?php

namespace Tendcom\Controllers\B2w;

use Tendcom\Controllers\ControllerBase;
use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Forms\Element\Submit;

class B2wController extends ControllerBase {

    public function initialize() {
        parent::initialize();
    }

    public function indexAction() {
        echo $user['id'];exit;
    }

I call $user variable in indexAction, but not working, see that the $user variable was initialized in main controller.

what's happening?

I have this reference: https://docs.phalcon.io/en/latest/controllers#initializing-controllers

I need help for understand.

Thanks



145.0k
Accepted
answer

First you need to learn some basics of PHP and OOP, best first to learn PHP and OOP and then start using framework. Variable $user ise defined in diffrent method. Php or any other language doesn't share variables between methods.

Wow!!!! Now I saw the shit I was trying to do, but it was good to wake up. Nice answer.

New:

<?php

class ControllerBase extends Controller {

    protected $user;

    public function initialize() {

        // set template
        $this->view->setTemplateAfter('tendcom');
        $this->getAssets();

        // set test active
        $this->view->isTest = ($this->config->isTest) ? true : null;

        // get url
        $baseUri = $this->url->getBaseUri();
        $this->view->baseUri = $baseUri;

        // verify if session exists
        if(!$this->session->get('userTendcom')) {
            header('Location: /login');
            return false;
        }

        // get user profile
        $user = $this->session->get('userTendcom');
        $this->setUser($user);

And

<?php

namespace Tendcom\Controllers\B2w;

use Tendcom\Controllers\ControllerBase;
use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Forms\Element\Submit;

class B2wController extends ControllerBase {

    public function initialize() {
        parent::initialize();
    }

    public function indexAction() {
        echo $this->user['id']; exit;
    }

My sincere apologies, and thank you.

To be honest i would put this code to some service.

Thanks for the suggestion, I'll improve this.