Reputation: 1279
Consider under my cpp\controllers\
I've 5 files like (AController.php
, BController.php
etc..)
Each controller has its own public variable like this..
AController.php --- public $variable='Testing';
BController.php --- public $variable='Bhuvanesh';
From my app\views\main.php
If A
controller is called I need the value Testing
. If B
controller is called I need Bhuvanesh
.
Its possible in yii2? Thanks in advance.
Upvotes: 1
Views: 1187
Reputation: 25312
You should read Yii2 Views Guide :
within the view you can get the controller object by the expression
$this->context
So, you should simply use this in your view :
$this->context->variable
Upvotes: 4
Reputation: 561
You could use the __construct() magic method? this function gets executed right when the class is used. If you make something like
public function __construct(){
echo $this->var; //echo out whatever you want here.
}
That's how i would do it.
Upvotes: 0
Reputation: 3008
Why don't you create a getter method with same name?
class AController {
public function getVariable() { return 'A'; }
}
class BController {
public function getVariable() { return 'B'; }
}
class CController {
public function getVariable() { return 'C'; }
}
Then you can call with
$controller->variable
Upvotes: 0