3

I have the following code in a controller:

<?php

class Student extends CI_Controller
{

function index()
{
    $data = $this->init->set();
    
    $this->parser->parse('include/header', $data);
    $this->parser->parse('student/student_index', $data);
    $this->parser->parse('include/footer', $data);
}

function planner()
{
    $data = $this->init->set();
    
    $this->parser->parse('include/header', $data);
    $this->parser->parse('student/student_cal', $data);
    $this->parser->parse('include/footer', $data);      
}

}
?>

As you can see, there's a lot of repetition here. Basically all of it. I already put my variables in a model so I only have to call the model function each time instead of putting the whole $data array at the start of each function. Anyway, I tried to reduce the repetition here by doing the following:

<?php

class Student extends CI_Controller
{

function index()
{
    $data = $this->init->set();
    
    $this->parser->parse('include/header', $data);
    
    switch($this->uri->segment(2))
    {
        case '': $this->home($data); break;
        case 'planner': $this->planner($data); break;
    }
    $this->parser->parse('include/footer', $data);
}

function home($data)
{
    $this->parser->parse('student/student_index', $data);
}


function planner($data)
{
    $this->parser->parse('student/student_cal', $data);
}

}
?>

This, somehow, works fine for my homepage. It parses the variables and there's no problem whatsoever. However, on the 'planner' page, I get errors:

Message: Missing argument 1 for Student::planner()

Message: Undefined variable: data

Message: Invalid argument supplied for foreach()

I'm quite sure I get these errors because the function somehow doesn't receive the $data array. I also read in the CI docs that the third segment in a URL gets passed as argument, and in this case the third segment is non-existent, thus nothing gets passed. However, the CI docs didn't tell me how I could pass my $data array from the index() function to my planner() function. I also wonder why the home function works fine, without errors.

2 Answers 2

3

Now, I don't see the reason for that refactoring if it's going to make the code really hard to look at. I'm not entirely sure what the parse function does, so the way I changed it was to actually pass the parameter as a string, but preferably I'd load the content into a buffer and pass it in that way. But here's some cleaner and hopefully readable removable of the duplication... and hopefully it works :).



class Student extends CI_Controller
{

  private function load_student_page($content){
      $data = $this->init->set();

      $this->parser->parse('include/header', $data);
      $this->parser->parse($content, $data);
      $this->parser->parse('include/footer', $data);

  }

  function index()
  {
    $this->load_student_page('student/student_index');
  }

  function planner()
  {
    $this->load_student_page('student/student_cal');
  }

}
Sign up to request clarification or add additional context in comments.

Comments

2

Like you said, CodeIgniter is trying to pass the third segment in as a parameter, but it doesn't exist.

You may need to use a "_remap" function.

class Student extends CI_Controller {

    public function _remap($method, $parameters)
    {
         $data = $this->init->set();
         $this->parser->parse('include/header', $data);

         switch($this->uri->segment(2))
         {
             case '': $this->home($data); break;
             case 'planner': $this->planner($data); break;
         }

         $this->parser->parse('include/footer', $data);
    }

}

1 Comment

Hmm, I tried this but I couldn't get it to work. I guess I'll mess around with it later; kinda lost some time trying that other 'solution' here. I was looking into the _remap function earlier, but it wouldn't really do what I wanted it to do. Perhaps I was using it wrong! :) I'll take a look at it again, and perhaps look up some templates.. I guess I really don't get this framework :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.