Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding codeigniter base controller structure
    primarykey
    data
    text
    <p>The following is a working example of how my Codeigniter website currently functions:</p> <p>Model:</p> <pre><code>&lt;?php class Default_model extends CI_Model { function __construct() { // Call the Model constructor parent::__construct(); } function get_link() { $query = $this-&gt;db-&gt;query('SELECT * FROM links LIMIT 5'); return $query-&gt;result(); } </code></pre> <p>Controller:</p> <pre><code>&lt;?php class Home extends CI_Controller { public function index() { $this-&gt;load-&gt;model('segment1/Page_model'); $data['link'] = $this-&gt;Page_model-&gt;get_link(); $this-&gt;load-&gt;view('page_view', $data); } } </code></pre> <p>View:</p> <pre><code>&lt;h2&gt;Link&lt;/h2&gt; &lt;ul&gt; &lt;?php if (isset($link)):?&gt; &lt;?php foreach ($link as $row):?&gt; &lt;li&gt;&lt;a href="&lt;?=$row-&gt;url?&gt;"&gt;&lt;?=$row-&gt;link?&gt;&lt;/a&gt;&lt;/li&gt; &lt;?php endforeach;?&gt; &lt;?php endif;?&gt; &lt;/ul&gt; </code></pre> <p>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...</p> <ol> <li>I autoload the Model, no problem</li> <li>The View file remains</li> <li>I alter the config.php file</li> </ol> <p>Controller:</p> <pre><code>&lt;?php class Home extends Main_Controller { public function index() { $this-&gt;load-&gt;model('segment1/Page_model'); $this-&gt;load-&gt;view('page_view', $data); } } </code></pre> <p>MY_Controller</p> <pre><code>&lt;?php class MY_Controller extends CI_Controller { function __construct() { parent::__construct(); } } </code></pre> <p>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...</p> <p>Main_Controller:</p> <pre><code>&lt;?php class Main_Controller extends MY_Controller { function __construct() { parent::__construct(); // // WHAT GOES IN HERE? // SERIOUSLY, HELP! // } } </code></pre> <p>Clearly, there's one big line of data missing from the original controller...</p> <pre><code>$data['link'] = $this-&gt;Page_model-&gt;get_link(); </code></pre> <p>How does it all tie up?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload