3

I am creating a form error logger, but since my register.php controller is getting full I thought of moving this to a helper but the problem is I cannot use $this.

Yes I have checked and there are some anwsers that fix this problem by using:

function test()
{
    $CI =& get_instance();
    $CI->load->database();
    echo $CI->db->hostname; // give the config name here (hostname).
}

(Quoted from Acess database config variables from a helper)

However my problem with this it that I can only use one model per function and seeing as my code I am trying to move is this:

function submitCheck() {
    $this->load->model("User");
    if(empty($post)) { //If anything is empty the view will be loaded
        $this->load->view('includes/header.php');
        $this->load->view('error_message.php'); 
        $this->load->view('includes/footer.php'); 

        if(empty($post['firstname'])) //This is for the error log
        {  
            $this->load->helper('array', 'error'); //Loading the helper
            echo $msg = errorLog('firstname');
            $this->load->view('error_message.php');
        }        

        if(empty($post['mail'])) //This is for the error log
        {
            $this->load->helper('error'); //Loading the helper
            echo $msg = errorLog('mail');
        }
    }
    else
    {
        echo "Everything is filled in";
    }
}

So if following the code example i'd have to make around 4/5 functions for every $this. Should I create a loader that in term loads other loaders or can I use the user_loader to load other views/models as well.

I am a starter with codeIgniter so I might've just thinked too difficult and there is an easy fix but I can't find it. Any help is appriciated

Thanks in advance!

Community
  • 1
  • 1
RDAxRoadkill
  • 414
  • 5
  • 24
  • 3
    If you need to use helper library more than one time then place this within constructor(_i.e. public function __construct()_) instead of calling redundantly – Narendrasingh Sisodia Jun 02 '15 at 09:19
  • If you mean adding the user_loader function to the constructor wouldn't I still need to load multiple models/views? If so do you know how I should change my loader function – RDAxRoadkill Jun 02 '15 at 09:23
  • 1
    You should understand the language you are using (before the framework). The topic of your question is somewhat misleading http://stackoverflow.com/questions/1523479/what-does-the-variable-this-mean-in-php – IROEGBU Jun 02 '15 at 09:28
  • I see, I shall read through that question then sorry for the misleading title. – RDAxRoadkill Jun 02 '15 at 10:19

1 Answers1

1

According to CodeIgniter documentation:

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.

It's not recommended to use helpers in your case. It's better to define your custom library for managing much of your codes.

Parag Tyagi
  • 8,780
  • 3
  • 42
  • 47
Mojtaba Rezaeian
  • 8,268
  • 8
  • 31
  • 54
  • So if I understand correctly I should move my function (submitCheck) to a library for example submitClass, then call the functions via `$this->load->submitClass->submitCheck();` – RDAxRoadkill Jun 02 '15 at 10:47