25

I'm a very newbie on CodeIgniter, and while I go on I run into problems that, in the procedural coding, were easy to fix

The current issue is: I have this controller

class Basic extends Controller {

    function index(){
        $data['title'] = 'Page Title';
        $data['robots'] = 'noindex,nofollow';
        $data['css'] = $this->config->item('css');
        $data['my_data'] = 'Some chunk of text';
        $this->load->view('basic_view', $data);
    }

    function form(){
        $data['title'] = 'Page Title';
        $data['robots'] = 'noindex,nofollow';
        $data['css'] = $this->config->item('css');
        $data['my_other_data'] = 'Another chunk of text';
        $this->load->view('form_view', $data);
    }
}

As you can see, some array items repeat over and over:

$data['title'] = 'Page Title';
$data['robots'] = 'noindex,nofollow';
$data['css'] = $this->config->item('css');

Isn't there a way to make them "global" in the controller, so that I have not to type them for each function? Something like (but this gives me error):

class Basic extends Controller {

    // "global" items in the $data array
    $data['title'] = 'Page Title';
    $data['robots'] = 'noindex,nofollow';
    $data['css'] = $this->config->item('css');

    function index(){
        $data['my_data'] = 'Some chunk of text';
        $this->load->view('basic_view', $data);
    }

    function form(){
        $data['my_other_data'] = 'Another chunk of text';
        $this->load->view('form_view', $data);
    }

}

Thnaks in advance!

5 Answers 5

41

What you can do is make "class variables" that can be accessed from any method in the controller. In the constructor, you set these values.

class Basic extends Controller {
    // "global" items
    var $data;

    function __construct(){
        parent::__construct(); // needed when adding a constructor to a controller
        $this->data = array(
            'title' => 'Page Title',
            'robots' => 'noindex,nofollow',
            'css' => $this->config->item('css')
        );
        // $this->data can be accessed from anywhere in the controller.
    }    

    function index(){
        $data = $this->data;
        $data['my_data'] = 'Some chunk of text';
        $this->load->view('basic_view', $data);
    }

    function form(){
        $data = $this->data;
        $data['my_other_data'] = 'Another chunk of text';
        $this->load->view('form_view', $data);
    }

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

2 Comments

@Dalen: Thanks for fixing that typo :-)
Thanksguys! I forgot in the meanwhile the question becouse I found that "$this->load->vars($array)" fits really nice for my examples... anyway the solution provided is still nicer if I have to pass the array between the class methods
18

You can setup a class property named data and then set it's default values into the contructor which is the first thing which is run when a new instance on Basic is created. Then you can reference to it with the $this keyword

class Basic extends Controller
{
   var $data = array();

   public function __construct()
   {
       parent::__construct();
       // load config file if not autoloaded
       $this->data['title'] = 'Page Title';
       $this->data['robots'] = 'noindex,nofollow';
       $this->data['css'] = $this->config->item('css');
   }

   function index()
   {
       $this->data['my_data'] = 'Some chunk of text';
       $this->load->view('basic_view', $this->data);
   }

   function form()
   {
       $this->data['my_data'] = 'Another chunk of text';
       $this->load->view('form_view', $this->data);
   }
}

4 Comments

You need to add parent::__construct(); to the constructor for this to work.
Right, and probably also load the config file if not already autoloaded
Better example than the chosen answer, although they look the same, conceptually look alike, a lot of repetitions are avoided here, also the idea of using a member variable by all functions is more visible in this example.
this is a great solution, very nice. some time things are to easy but we don't know about them and face a lot's of problem. Thanks for your answer.
6

hey thanks here's my snipet it's a global variable holding a view

/* Location: ./application/core/MY_Controller  */

class MY_Controller extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->data = array(
            'sidebar' => $this->load->view('sidebar', '' , TRUE),
        );
    }

}

/* Location: ./application/controllers/start.php */
class Start extends MY_Controller {

    function __construct()
    {       
        parent::__construct();
    }

    public function index()
    {
        $data = $this->data;

        $this->load->view('header');
        $this->load->view('start', $data);
        $this->load->view('footer');
    }
}

Comments

2

Even though its been so long. It can be helpful to other you can use $this->load->vars($data); in core MY_controller to make $data array available in all views.

/* Location: ./application/core/MY_Controller  */

class MY_Controller extends CI_Controller {

function __construct()
{
    parent::__construct();
    $data['title'] = 'Page Title';
    $data['robots'] = 'noindex,nofollow';
    $data['css'] = $this->config->item('css');
    $this->load->vars($data);
}

}

 /* Location: ./application/controllers/start.php */
class Start extends MY_Controller {

function __construct()
{       
    parent::__construct();
}

public function index()
{
    $data['myvar'] = "mystring";

    $this->load->view('header');
    $this->load->view('start', $data);
    $this->load->view('footer');
}
 }

Comments

0

Why not user a helper?

File:

/application/helpers/meta_helper.php

Content:

<?php 
function meta_data() {
return array("title" => null, "robots" => "noindex, nofollow" );
}

In your controller:

class Basic extends Controller {

    function __construct(){
        parent::__construct();
        $this->load->helper('meta');
    }    

    function index(){
        $data['meta'] = meta_data(); //associate the array on it's own key;

        //if you want to assign specific value
        $data['meta']['title'] = 'My Specific Page Title';

        //all other values will be assigned from the helper automatically

        $this->load->view('basic_view', $data);
    }

And in your view template:

 <title><?php $meta['title']; ?></title>

Will output:

<title>My Specific Page Title</title>

Hope that makes sense :-)!

Comments

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.