dongtu7205 2016-10-23 02:59
浏览 58

CakePHP如何创建函数并从视图中调用它们

How can I create functions that can be called in view (like in the index.ctp)?

I just wrote a function in one of my controllers:

public function getName($id) {
    $name = $users
      ->find()
      ->where(['id' => $id ])
      ->first()
      ->username;
    return $name;
}

The function should return the username according to the $id passed as a parameter. I just want to know how can I call a function from within my views. I'm getting this error:

"Error: Call to undefined function getName()"

  • 写回答

2条回答 默认 最新

  • doute7910 2016-10-23 05:14
    关注

    You can do it, but it is against MVC-model and is not in anyway recommended.

    Namespace\ControllerName::getName(1)
    

    You should really consider taking look CakePHP manual and understand, how to set variables to view from controller http://book.cakephp.org/3.0/en/views.html#setting-view-variables

    Other option would be making AJAX-request to your controller, if you want get data based on for e.g. user actions in view.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?