vendredi 15 mai 2015

MVC framework - use of '$this'

Is it correct to call controller methods and access its attributes through $this variable when inside a view?

Lets say Im running 'index' view from 'Calcs' controller:

Calcs controller:

class Calcs extends Controller{

    public function index(){

        $this->set('number', 1);
        $this->set('number2', 4);
        $this->set('number3', 5);
        $this->set('number4', 2);

    }

    public function doComplexStuff($n1, $n2){
        return $n1 + $n2;
    }

}

View "index.phtml":

// Html ...
echo $this->doComplexStuff($number, $number2); //5
echo $this->doComplexStuff($number3, $number4); //7

Then i have another controller that also uses 'doComplexStuff' from 'Calcs'

class Another extends Controller{

    public function randomView(){

        $calcsController = ControllerFactory::getController('Calcs');
        $myRandomNumber = $calcsController->doComplexStuff(rand(), rand());
        $this->set('myRandomNumber', $myRandomNumber);

    }

}

Aucun commentaire:

Enregistrer un commentaire