php codeigniter - 在控制器中使用助手不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11966355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
codeigniter - using helper in controller doesn't work
提问by DijkeMark
I need to use a function in multiple controllers. So I though about using a custom helper, but it seems I can't get it to work. (It works in the view, but I need it in the controller)
我需要在多个控制器中使用一个函数。所以我想使用自定义助手,但似乎我无法让它工作。(它在视图中工作,但我需要它在控制器中)
It gives me following Fatal Error:
它给了我以下致命错误:
Fatal error: Call to undefined method Developers::checkIfLoggedIn() in /application/controllers/developers.php on line 12
致命错误:在第 12 行调用 /application/controllers/developers.php 中未定义的方法 Developers::checkIfLoggedIn()
Is it a smart move to use a helper to use a function in multiple controllers, or should I do it otherwise.
使用助手在多个控制器中使用功能是明智之举,还是我应该这样做。
Thanks in Advance,
Mark
提前致谢,
马克
EDIT:
Controller file:
编辑:
控制器文件:
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Developers extends CI_Controller
{
public function __construct()
{
parent::__construct()
$this->load->helper('form');
$this->load->helper('url');
$this->load->helper('login');
//helper function
checkIfLoggedIn($this->session->userdata('loggedIn'));
}
}
Helper file:
帮助文件:
if (!defined('BASEPATH')) exit('No direct script access allowed');
if (!function_exists('checkIfLoggedIn'))
{
function checkIfLoggedIn($session_loggedIn)
{
$loggedIn = $session_loggedIn;
if($loggedIn == false)
{
redirect('login/');
}
}
}
}
}
回答by The Alpha
In your controller you are using it in wrong way, it's not a method of controller so you can't use $thisto call it.
在您的控制器中,您以错误的方式使用它,它不是控制器的方法,因此您不能使用$this它来调用它。
A helper can be loaded anywhere within your controller functions (or even within your View files, although that's not a good practice), as long as you load it before you use it. You can load your helpers in your controller constructor so that they become available automatically in any function, or you can load a helper in a specific function that needs it.
可以在您的控制器函数中的任何位置(甚至在您的视图文件中,尽管这不是一个好习惯)加载助手,只要您在使用它之前加载它。您可以在控制器构造函数中加载您的助手,以便它们在任何函数中自动可用,或者您可以在需要它的特定函数中加载助手。
To load a helper you can use
要加载一个助手,您可以使用
$this->load->helper('name'); // name is the name of the file without extention
Unlike most other systems in CodeIgniter, Helpers are not written in an Object Oriented format. They are simple, procedural functions. Each helper function performs one specific task, with no dependence on other functions.
与 CodeIgniter 中的大多数其他系统不同,Helper 不是以面向对象的格式编写的。它们是简单的过程函数。每个辅助函数执行一项特定任务,不依赖于其他函数。
So, to call a helper function in a controller you should not use
因此,要在控制器中调用辅助函数,您不应该使用
$this->function_name();
instead use
而是使用
function_name();
For example if you have a helper function in a helper file named myCustomHelper.phpas follows
例如,如果您在一个名为myCustomHelper.php如下的帮助文件中有一个帮助函数
function myHelper()
{
// code
}
then you can load it in the controller and call it as follows
然后你可以在控制器中加载它并按如下方式调用它
$this->load->helper('myCustomHelper');
myHelper(); // call the function
but it's better to load helpers in the constructor so it'll be available through the whole script.
但最好在构造函数中加载助手,这样它就可以在整个脚本中使用。
Update:If your helper file's name is login_helper.phpthen you can use it in your controller as follows
更新:如果您的帮助文件的名称是,login_helper.php那么您可以在控制器中使用它,如下所示
$this->load->helper('login_helper');
checkIfLoggedIn($this->session->userdata('loggedIn'));
回答by roelleor
Ok, I know this question has been asked 5 months ago but maybe some people will find this useful. I had just had the same problem and discovered that the filename of my helper functions was a filename that was already used by CodeIgniter.
好的,我知道这个问题在 5 个月前就有人问过了,但也许有些人会觉得这很有用。我刚刚遇到了同样的问题,发现我的辅助函数的文件名是 CodeIgniter 已经使用的文件名。
So if you don't get the warning: 'Unable to load the requested file', but instead get the warning: 'Fatal error: Call to undefined function [function_name], you're probably using a filename that already exists natively.
因此,如果您没有收到警告:“无法加载请求的文件”,而是收到警告:“致命错误:调用未定义的函数 [function_name],您可能正在使用本机已存在的文件名。
回答by P2000
On a related matter: caution when putting $this->load in the __constructor: in PHP you must then then explicitly call parent::__construct(); otherwise the parent constructor is no longer called, leaving $this->load undefined. The above solution does it correctly, but it's easy to overlook.
关于相关问题:将 $this->load 放入 __constructor 时要小心:在 PHP 中,您必须然后显式调用 parent::__construct(); 否则父构造函数不再被调用, $this->load 未定义。上面的解决方案是正确的,但很容易被忽视。
class My extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url'); }
If you don't do this, you'll get the error message that My::$load does not exist and the helper will not load.
如果您不这样做,您将收到错误消息 My::$load 不存在并且帮助程序不会加载。
回答by Aman Dhanda
Helpers are the ideal way to declare global functions in codeigniter and what you are doing is correct. Following are just few points that will help you.
Helpers 是在 codeigniter 中声明全局函数的理想方式,并且您所做的是正确的。以下几点可以帮助您。
Load all helpers through single line instead of separate lines.
$this->load->helper('form'); $this->load->helper('url'); $this->load->helper('login');$this->load->helper(array('form','url','login'));If you want to make it global across all the controller files, then you can put it in the autoload.php file located in '
config/' directory. Update the$autoload['helper']variable as follows:$autoload['helper'] = array('url','site');
通过单行而不是单独的行加载所有助手。
$this->load->helper('form'); $this->load->helper('url'); $this->load->helper('login');$this->load->helper(array('form','url','login'));如果你想让它在所有控制器文件中成为全局的,那么你可以把它放在位于 '
config/' 目录的 autoload.php 文件中。更新$autoload['helper']变量如下:$autoload['helper'] = array('url','site');
Source: How to create & call global PHP functions in CodeIgniter
回答by Muhammad Raheel
Instead create a library class and define your function there. Then load the library in the controller and call the function of library. You can load library in any controller and use its methods.
而是创建一个库类并在那里定义您的函数。然后在控制器中加载库并调用库的函数。您可以在任何控制器中加载库并使用其方法。

