CodeIgniter - 从视图内部调用函数 [英] CodeIgniter - Calling a function from inside a view

查看:87
本文介绍了CodeIgniter - 从视图内部调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从视图中调用位于控制器中的函数?

Is it possible to call a function which is located in a controller from a view?

这是我在我的控制器中的示例

This is what i have in my controller, as an example

function checkKeyExists($userid, $key){
}

然后在我的视图中,我有以下

Then inside my view i have the following

if(checkKeyExists($row->id, $role->key)){
}

但是当我运行它,它说,checkKeyExists没有定义。

But when I run it, it says that checkKeyExists is not defined.

如果任何人都可以让我知道我该怎么做,这将是巨大的。 >

If anyone can let me know how I could do this, that would be great.

推荐答案

视图并不意味着调用控制器操作。
颠倒你的逻辑,在控制器中调用该函数,并将其设置为发送到视图的变量。然后你可以有if语句检查你的视图模板中的变量。

Views are not meant to call controller actions. Reverse your logic, call that function in the controller and set it to a variable you sent to the view. Then you can have the if statement check that variable in your view template.

如果这不适合你,也许一个助手是你需要的: http://ellislab.com/codeigniter/user-guide/general/helpers.html

If that doesn't work for you, maybe a helper is what you need: http://ellislab.com/codeigniter/user-guide/general/helpers.html

这篇关于CodeIgniter - 从视图内部调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆