Welcome Guest, Not a member yet? Register   Sign In
Repeating piece of code
#1

[eluser]lopetzi[/eluser]
Hello world,

I am new to CI, but i really like it.

I'm having one small problem:

I have a piece of code that i will have to repeat in almost every controller. Is there any way to put that code in a file and execute it... ?

home.php
Code:
class Home extends Controller {

    function Home()
    {
        parent::Controller();    
    }
    
    function index()
    {
        // model is autoloaded
        $general = $this->general->get_general();
        $template['description'] = $general->description;
        $template['keywords'] = $general->keywords;
        $template['price'] = $general->price;
        $user = 'Guest';
        $template['welcome'] = 'Welcome, ' . $user;
        // all the code above will repeat in every controller
        
        $template['title'] = 'Titlu';
        $template['heading'] = 'HOME';
        $template['left'] = $this->load->view('home','',true);
        $template['right'] = $this->load->view('newsform','',true);
    
        $this->load->view('main', $template);
        
        $this->output->enable_profiler(TRUE);
    }
}

I've tried to copy-paste that code into a library and then load the library, but i get an error...

How do you handle the code that repeats in your controllers ?

Thanks,
George
#2

[eluser]TheLoops[/eluser]
Just subclass "Controller", add the function to it and then use:

Code:
class Home extends MySubclassedController {
. . .
}
for your controllers and they all will have know that function.
#3

[eluser]lopetzi[/eluser]
I made a MY_Controller library:

Code:
<?php

class MY_Controller extends Controller {

    function auto()
    {
        $user = 'Guest';
        
        $general = $this->general->get_general();
        
        $template['welcome'] = 'Welcome, ' . $user;
        $template['description'] = $general->description;
        $template['keywords'] = $general->keywords;
        $template['price'] = $general->price;
    }
    
}

and now home looks like this:
Code:
<?php

class Home extends MY_Controller {

    function Home()
    {
        parent::MY_Controller();    
    }
    
    function index()
    {
        $template['heading'] = 'HOME';
        $template['title'] = 'Titlu';
        $template['left'] = $this->load->view('home','',true);
        $template['right'] = $this->load->view('newsform','',true);
    
        $this->load->view('main', $template);
        
        $this->output->enable_profiler(TRUE);
    }
}

I get errors that variables in the library are not set.
I don't know how to use the data in the library Sad

For example: how do i echo $user from the library?

Thanks
#4

[eluser]loathsome[/eluser]
Did you call the auto function? Use the parent keyword to do so. In my current project, some of my controllers look like this:

Code:
class Blah extends BaseController {

function Blah() {
   parent::BaseController();
}

function index() {
  parent::data();
}

And the BaseController looks something like this:
Code:
class BaseController extends Controller {

function BaseController() {
   parent::Controller();
}

function data(){
   return $data = null; // blabla, fetches data here
}

}

Best of luck.
#5

[eluser]lopetzi[/eluser]
I was doing it right, but i didn't know how to fetch data from my controller...

Thanks




Theme © iAndrew 2016 - Forum software by © MyBB