In cakephp 3 x, how to call controller function in ctp file?
For example: function userinfo in UsersController, how we call the userinfo function in anyfunction of SaleController and also the userinfo in promotion.ctp ?
Thanks,
In cakephp 3 x, how to call controller function in ctp file?
For example: function userinfo in UsersController, how we call the userinfo function in anyfunction of SaleController and also the userinfo in promotion.ctp ?
Thanks,
Hello.
I think this violates the mvc pattern logic. All data that you want to display, should be tranferred to the view from the controller (which therefere calls a function from the model (small controllers/fat models)).
Maybe, if you want to access or repeat some output from many positions, create a view, cell or element (which also gets the data from the callee).
Hope that helps a bit.
TheMiller is right. The is not the correct aproach.
If you need “userInfo” you should fill the as a variable and set it to view.
$userInfo = $this->users->getUserInfo(); //note that a function from model is used here
$this->set(‘userinfo’,$userInfo):
and in promotion.ctp you have the $userinfo variable filled.
Repeat the process on SaleController.
Here you get the info before calling the view,
if you need userinfo everywhere just use AppController::beforeRender method, set any var you want to the view(.ctp files)
if you need a generic function like config to avoid writing every time \Cake\Core\Configure::read(‘key’) in a view just add a function into the bootstrap.php file
if you need a method that you can use on many controller/model/table/entity/any other layer just create a Trait