codeigniter - 在控制器中使用助手不工作 [英] codeigniter - using helper in controller doesn't work

查看:95
本文介绍了codeigniter - 在控制器中使用助手不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在多个控制器中使用一个函数。
所以我虽然使用自定义助手,但似乎我不能让它工作。 (它在视图中工作,但我需要它在控制器中)



它给我以下致命错误:



<

致命错误:调用未定义的方法Developers :: checkIfLoggedIn()in
/application/controllers/developers.php on line 12


这是一个聪明的方法使用助手在多个控制器中使用一个函数,否则我应该这样做。



感谢提前,

标记



编辑:

控制器档案:

  if(!defined('BASEPATH'))exit('不允许直接脚本访问); 

类开发人员扩展CI_Controller
{
public function __construct()
{
parent :: __ construct()
$ this-> load-> helper('form');
$ this-> load-> helper('url');
$ this-> load-> helper('login');

//帮助函数
checkIfLoggedIn($ this-> session-> userdata('loggedIn'));
}
}

助手文件:

  if(!defined('BASEPATH'))exit('不允许直接脚本访问); 

if(!function_exists('checkIfLoggedIn'))
{
function checkIfLoggedIn($ session_loggedIn)
{
$ loggedIn = $ session_loggedIn;
if($ loggedIn == false)
{
redirect('login /');
}
}
}

}

解决方案

在你的控制器中你错误地使用它,它不是一个控制器的方法,所以你不能使用 $


帮助程序可以加载到控制器函数中的任何位置(或
即使在你的View文件中,虽然这不是一个好的做法),作为
长,你加载它之前,使用它。你可以加载你的助手在
你的控制器构造函数,使他们在任何函数中自动可用的
,或者你可以加载一个帮助器在一个特定的
函数需要它。


要加载助手,您可以使用

  $ this-> load-> helper('name'); // name是没有扩展的文件的名称




CodeIgniter中的系统,Helpers不是用
写的面向对象格式。它们是简单的过程函数。每个
帮助函数执行一个特定任务,而不依赖于
其他函数。


您不应该使用控制器中的辅助函数

  $ this-> function_name(); 

而不要使用

  function_name(); 

例如,如果您在名为 myCustomHelper的帮助文件中具有辅助函数。 php 如下

  function myHelper()
{
// code
}

那么您可以将其加载到控制器中,并按如下方式调用

  $ this-> load-> helper('myCustomHelper'); 
myHelper(); //调用函数

但是最好在构造函数中加载helpers,

更新:如果您的助手文件名为 login_helper.php 那么你可以在你的控制器中使用它如下

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

在此处了解详情


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

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

}

解决方案

In your controller you are using it in wrong way, it's not a method of controller so you can't use $this to call it.

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.

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.php as follows

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.php then you can use it in your controller as follows

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

Read more here.

这篇关于codeigniter - 在控制器中使用助手不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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