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 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.phalconphp.com/en/latest/controllers#initializing-controllers

I need help for understand.

Thanks



133.2k
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.