# Cakephp 3 x call controler action in view(ctp file)

**URL:** <https://discourse.cakephp.org/t/cakephp-3-x-call-controler-action-in-view-ctp-file/813>\
**Category:** Need Help\
**Created:** [July 26, 2016, 10:18am UTC](https://discourse.cakephp.org/t/cakephp-3-x-call-controler-action-in-view-ctp-file/813 "2016-07-26T10:18:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![LyBun](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@LyBun](https://discourse.cakephp.org/u/LyBun)\
**Post date:** [July 26, 2016, 10:18am UTC](https://discourse.cakephp.org/t/cakephp-3-x-call-controler-action-in-view-ctp-file/813/1 "2016-07-26T10:18:31Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![TheMiller](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@TheMiller](https://discourse.cakephp.org/u/TheMiller)\
**Post date:** [July 26, 2016, 6:00pm UTC](https://discourse.cakephp.org/t/cakephp-3-x-call-controler-action-in-view-ctp-file/813/2 "2016-07-26T18:00:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Din0saur](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/din0saur/32/323_2.png) [@Din0saur](https://discourse.cakephp.org/u/Din0saur)\
**Post date:** [July 26, 2016, 7:31pm UTC](https://discourse.cakephp.org/t/cakephp-3-x-call-controler-action-in-view-ctp-file/813/3 "2016-07-26T19:31:30Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![flashios09](https://avatars.discourse-cdn.com/v4/letter/f/eb9ed0/32.png) [@flashios09](https://discourse.cakephp.org/u/flashios09)\
**Post date:** [September 2, 2016, 12:23pm UTC](https://discourse.cakephp.org/t/cakephp-3-x-call-controler-action-in-view-ctp-file/813/4 "2016-09-02T12:23:52Z")

</div>

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
