Welcome Guest, Not a member yet? Register   Sign In
Understanding base classes structure and make-up
#1

[eluser]epseix[/eluser]
The following is a working example of how my Codeigniter website currently functions:

Model:
Code:
<?php
class Default_model extends CI_Model
{

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }


    function get_link()
    {
  $query = $this->db->query('SELECT * FROM links LIMIT 5');
  return $query->result();
    }

Controller:
Code:
<?php
class Home extends CI_Controller {

    public function index()
    {
  $this->load->model('segment1/Page_model');
  $data['link'] = $this->Page_model->get_link();
  $this->load->view('page_view', $data);
    }
}

View:
Code:
<h2>Link</h2>
    <ul>
   &lt;?php if (isset($link)):?&gt;
    &lt;?php foreach ($link as $row):?&gt;
     <li><a href="&lt;?=$row-&gt;url?&gt;">&lt;?=$row->link?&gt;</a></li>
    &lt;?php endforeach;?&gt;
   &lt;?php endif;?&gt;
    </ul>

I want to begin using a base controller for the above example, and while I've followed a few online examples - I can't quite get it right, and I'd appreciate some guidance...

1) I autoload the Model, no problem
2) The View file remains unchanged
3) I alter the config.php file

Controller:
Code:
&lt;?php
class Home extends Main_Controller {

    public function index()
    {
  $this->load->model('segment1/Page_model');
  $this->load->view('page_view', $data);
    }
}

MY_Controller
Code:
&lt;?php
class MY_Controller extends CI_Controller
{
function __construct()
{
  parent::__construct();
}
}

Now, here's where I get stuck - I can't quite figure out exactly what goes in the Main_Controller, and how it's structured...

Main_Controller:
Code:
&lt;?php
class Main_Controller extends MY_Controller
{
function __construct()
{
  parent::__construct();
  //
  // WHAT GOES IN HERE?
  // SERIOUSLY, HELP!
  //
}
}

Clearly, there's one big line of data missing from the original controller...

Code:
$data['link'] = $this->Page_model->get_link();

How does it all tie up?
#2

[eluser]Syllean[/eluser]
Code:
/**
* Gets user input from contact_view
*
* @access public
* @link            base_url()/application/core/MY_Controller.php
*/
class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        //Constructors are useful if you need to set some default values,
        //or run a default process when your class is instantiated.
        //Constructors can't return a value, but they can do some default work.}
        //eg: load your default model
        $this->load->model('Default_model');
    }

    public function link()
    {
        $link = "www.codeigniter.com"
        return $link;
    }
}

Code:
class Home extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        // call a function in your base class
        $link = $this->link();
    }
}

You don't need Main_controller unless you are using it to extend your My_controller for specific controllers only. In Phil Sturgeon's example he has an admin controller to provide functions for a control panel.




Theme © iAndrew 2016 - Forum software by © MyBB