Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeIgniter Models & Controllers Confusion
    primarykey
    data
    text
    <p>Today i've decided that I should start relying on a PHP framework because writing from scratch every time is really exhausting. As my framework I've chosen CodeIgniter, and I want to say It's amazing and easy to use. But I have some questions and confusion all over. I'm not pretty sure how to structre my website since I don't know when to use Models, and when to use Controllers.<br>What I have right now is: <br><br></p> <h1>Pages Controller</h1> <pre><code>// PAGES CONTROLLER // As its name, this controller simply loads pages by a url query. class Pages extends CI_Controller { /* * Constructor - loads the variables model. */ public function __construct() { parent::__construct (); $this-&gt;load-&gt;model ( 'variables' ); } /* * Displays a page by its name. */ public function view($page = 'home') { if (! file_exists ( "application/views/pages/$page.php" )) { show_404 (); } $data ['title'] = ucfirst ( $page ); $data ['variables'] = $this-&gt;variables; $this-&gt;load-&gt;view ( "templates/header", $data ); $this-&gt;load-&gt;view ( "pages/$page", $data ); $this-&gt;load-&gt;view ( "templates/footer", $data ); } } </code></pre> <p><br></p> <h1>Variables Model</h1> <pre><code>// VARIABLES MODEL // Used like a "mysql variables", all the data is taken from a table contains 2 // rows: id and value. and those variables are mostly used for site settings. class Variables extends CI_Model { /* * Constructor, simply loads the database. */ public function __construct() { $this-&gt;load-&gt;database (); } /* * Gets a variable stored in the database. */ public function get($id) { $query = $this-&gt;db-&gt;get_where ( 'variables', array ( 'id' =&gt; $id ) ); return $query-&gt;row_array ()["value"]; } /* * Sets a variable stored in the database. */ public function set($id, $value) { $this-&gt;db-&gt;where ( 'id', $id ); $this-&gt;db-&gt;update ( 'variables', array ( 'value' =&gt; $value ) ); } } </code></pre> <p>Am I using the correct hierarchy? Is there something I could change?</p> <p><strong>Now for my main question: lets say for example that I want to add a membership functionality for my websites. Should I do the following? :</strong></p> <p>Member Controller - Managing the current member, and all the forms actions are leading to this controller which communicates with the Members model(see below).</p> <p>Members model - Handles all the database handling, functions like: login, register, getMember.</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