2012-08-15 77 views
4

我需要在多个控制器中使用一个函数。 所以我虽然关于使用自定义帮手,但似乎我无法让它工作。 (它的工作原理的观点,但我需要它的控制器)codeigniter - 在控制器中使用帮助器不起作用

它给了我下面的致命错误:

Fatal error: Call to undefined method Developers::checkIfLoggedIn() in /application/controllers/developers.php on line 12

它是一个明智的举动使用一个辅助的多个控制器使用的功能,否则我应该这样做。

由于事先
马克

编辑:
Controller文件:

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')); 
    } 
} 

助手文件:

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/'); 
     } 
    } 
} 

}

+2

我有同样的问题,我创建了一个基本的控制器与该功能,并扩大在每个控制器,因为助手是意见 – EaterOfCode 2012-08-15 08:47:25

+0

好吧,谢谢。我会认为我会那样做 – DijkeMark 2012-08-15 08:56:49

+0

@EaterOfCorpses,助手不仅仅是'views',你也可以在控制器中使用它。 [阅读](http://codeigniter.com/user_guide/general/helpers.html)。 – 2012-08-15 09:34:51

回答

0

而是创建一个库类并在那里定义你的函数。然后在控制器中加载库并调用库函数。您可以在任何控制器中加载库并使用其方法。

+0

帮助程序可以在控制器中用于CI [CodeIgniter助手](http://codeigniter.com/user_guide/general/helpers.html) 。 – 2012-08-15 10:28:32

15

在您的控制器中,您以错误的方式使用它,它不是控制器的一种方法,因此您不能使用$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.

加载一个帮手,你可以使用

$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.

因此,要调用的控制器的辅助函数,你不应该使用

$this->function_name(); 

改用

function_name(); 

例如,如果你有一个名为myCustomHelper.php一个辅助文件中的辅助函数如下

function myHelper() 
{ 
    // code 
} 

那么你就可以在控制器中加载它,并把它称为如下

$this->load->helper('myCustomHelper'); 
myHelper(); // call the function 

,但它是更好地加载帮助器在构造函数中,所以它可以通过整个脚本来使用。

更新:如果你的助手文件的名称是login_helper.php那么你可以如下

$this->load->helper('login_helper'); 
checkIfLoggedIn($this->session->userdata('loggedIn')); 

Read more here用它在你的控制器。

+0

我试着用$ this->调用函数,但它仍然找不到函数 – DijkeMark 2012-08-15 09:46:44

+0

@DijkeMark,不,你不应该用'$ this'调用函数,只需使用函数名称调用,即'myFunctionName(); '。 – 2012-08-15 10:20:30

+0

我很抱歉,那就是我的意思。 :)我tpyed,但我的意思是没有。但是,它仍然无法找到我的功能。 – DijkeMark 2012-08-15 10:22:31

4

好吧,我知道这个问题已被问到5个月前,但也许有些人会觉得这有用。我刚刚遇到同样的问题,并发现我的帮助函数的文件名是CodeIgniter已经使用的文件名。

因此,如果您没有收到警告:'无法加载请求的文件', ,而是得到警告:'致命错误:调用未定义函数[function_name],您可能使用的文件名是已经存在于本地。

+1

你绝对的传奇。刚刚遇到这个问题,你可能节省了我的时间:D – 2014-01-11 03:34:00

0

关于相关问题:在$ _-构造函数中放置$ this-> load时需要注意:在PHP中,您必须显式调用parent :: __ construct();否则父构造函数不再被调用,使$ this-> load未定义。上述解决方案正确,但很容易忽略。

class My extends CI_Controller { 
public function __construct() { 
parent::__construct(); 
$this->load->helper('url'); } 

如果您不这样做,您将收到错误消息:My :: $加载不存在,助手将不会加载。