2012-08-15 70 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 :: $加載不存在,助手將不會加載。