0

How can I load fragments into a function to reduce duplicate code.

For instance if I used the following code;

 public function site()
    {
        $this->load->model('model_get');
        $data["results"] = $this->model_get->getData(__FUNCTION__);
        $this->load->view('view_head', $data);
        $this->load->view('view_nav');
        $this->load->view('view_content_' . __FUNCTION__, $data);
        $this->load->view('view_footer');
    }

How could I reduce it to something light like:

 public function site()
    {
        command $this->load->model('site_loader');
        //Function specific code
    }

With site_loader.php containing:

        $this->load->model('model_get');
        $data["results"] = $this->model_get->getData(__FUNCTION__);
        $this->load->view('view_head', $data);
        $this->load->view('view_nav');
        $this->load->view('view_content_' . __FUNCTION__, $data);
        $this->load->view('view_footer');

Working code based on totymedli suggestion.

 Controller
 public function site()
    {
        $pagename = __FUNCTION__;
        include 'resources/scripts/site_loader.php';
    }

 Site Loader
 <?php
        $this->load->model('model_get');
        $data["results"] = $this->model_get->getData($pagename);
        $this->load->view('view_head', $data);
        $this->load->view('view_nav');
        $this->load->view('view_content_' . $pagename, $data);
        $this->load->view('view_footer');
 ?>

1 Answers1

-1

If you really want to do this, you can copy that code to a PHP file than include where you need it. Interesting part from the manual:

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

The code you want to include:

<?php
//site_loader.php
$this->load->model('model_get');
$data["results"] = $this->model_get->getData(__FUNCTION__);
$this->load->view('view_head', $data);
$this->load->view('view_nav');
$this->load->view('view_content_' . __FUNCTION__, $data);
$this->load->view('view_footer');
?>

The controller:

public function site()
{
   include 'site_loader.php';
}

Note: There is require, require_once and include_once too. Pick the one that is the most suitable for your project. Help for the choice.

Community
  • 1
  • 1
totymedli
  • 29,531
  • 22
  • 131
  • 165
  • I am going to downvote it as separating code into files and relying on `include` is nowhere close to actually structuring your code to separate functions/classes/methods and calling them when necessary. Some people even take approach of compiling the code into single PHP file, and this is where your approach would fail. Why complicate your life when you can simply have your code separated into logical pieces without relying on executing whole files together. Yes, you can _return_ values from a file and assign it to variable, but extending it with parameters is ugly as hell. – Tadeck Sep 02 '13 at 23:38
  • @Tadeck I know, thats why I started with `If you really want to do this`. I just gave a solution. Maybe I should emphasize a bit more that this isn't the best solution, but if you fell that there are misconceptions in the basis of the question, than you can still write this in a new answer and give a better solution than mine. – totymedli Sep 02 '13 at 23:43
  • Because I have to repeat the above small snippet of code for each view, and i have about 40 at the moment and need around 30 more, using this method has done what libraries and models are unable to do in codeigniter, or at least in any way i have been unable to do, and reduced the bulk of my controller by quite alot. – Liam Dolman Sep 02 '13 at 23:47
  • @LiamDolman: That is not true - you can create your own method containing what you otherwise store in a separate file. You can even utilize inheritance (and call eg. `parent::site()` in order to execute what you want to include in a file). Take the OOP approach and use its strong sides, do not make your code's future maintainers scream. – Tadeck Sep 02 '13 at 23:59
  • @Tadec Okay, doing it the OOP way has worked for most of the code but as i use __FUNCTION__ to automatically fill in some code how can i replace this functionality? – Liam Dolman Sep 03 '13 at 00:33
  • @LiamDolman: You can pass parameters to functions/methods like this: `parent::do_some_common_thing(__FUNCTION__)`. Does that suit your needs? Believe me, when your application grows, it is better to have well designed code that you can then extend. OOP meets that goal more than raw `include`s. – Tadeck Sep 03 '13 at 00:54
  • I get undefined variable errors on both: self::build_site(__FUNCTION__); and $data["page"] = __FUNCTION__; self::build_site($data); I'm probably missing something very basic I'm sure of it... Also, I agree, I defiantly want to limit the externally referenced resources, I'm just new to oop and mvc. – Liam Dolman Sep 03 '13 at 01:27