0

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.
What I have right now is:

Pages Controller

// 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->load->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->variables;

        $this->load->view ( "templates/header", $data );
        $this->load->view ( "pages/$page", $data );
        $this->load->view ( "templates/footer", $data );
    }
}


Variables Model

// 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->load->database ();
    }
    /*
     * Gets a variable stored in the database.
     */
    public function get($id) {
        $query = $this->db->get_where ( 'variables', array (
                'id' => $id 
        ) );
        return $query->row_array ()["value"];
    }
    /*
     * Sets a variable stored in the database.
     */
    public function set($id, $value) {
        $this->db->where ( 'id', $id );
        $this->db->update ( 'variables', array (
                'value' => $value 
        ) );
    }
}

Am I using the correct hierarchy? Is there something I could change?

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? :

Member Controller - Managing the current member, and all the forms actions are leading to this controller which communicates with the Members model(see below).

Members model - Handles all the database handling, functions like: login, register, getMember.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • Use Controllers for handling requests and passing data to view while use models for all your DB interaction and implement all business logics of your system in models use views only for displaying data – M Khalid Junaid Dec 12 '13 at 21:40
  • @MKhalidJunaid Thank you very much for the quick answer, I already know what you just told me, but my main question is if I'm right with the example I gave. – Orel Bitton. Dec 12 '13 at 21:44
  • You should also take a look at HTML5 boiler plate for building website structure.... http://ariok.github.io/codeigniter-boilerplate/ – Vaibhav Dec 13 '13 at 10:41

1 Answers1

2

I just want to suggest in your controller you can do this in two lines:

    $this->load->view ( "templates/header", $data );
    $this->load->view ( "pages/$page", $data );
    $this->load->view ( "templates/footer", $data );

Since you can load your view inside a view then you can try: In your view e.g template.php

    $this->load->view('templates/header');
    $this->load->view($main_content);
    $this->load->view('templates/footer');

And you might want your controller on not having too much code because all heavy task should be in your model so in your controller :

   $data['main_content'] = $page;
    $this->load->view('templates/template',$data);
leonardeveloper
  • 1,813
  • 1
  • 34
  • 58
  • Thanks for the suggestion! So your'e simply assigning the page name the main_content variable, but how do I display it in the view? what function do I need to use? – Orel Bitton. Dec 12 '13 at 22:01
  • `$data['main_content']` is equal to the page that will be loaded. – leonardeveloper Dec 12 '13 at 22:03
  • Try to check this series and never miss one http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-day-1/ – leonardeveloper Dec 12 '13 at 22:05
  • I'm a bit confused, $data['main_content'] is equal to the page name or to its content? And if it equals to the page name, do I load the page from the view itself? – Orel Bitton. Dec 12 '13 at 22:05
  • 1
    yes it is. since in your view you loaded the `header` and the `footer` and you just need to include the `main_content` of your page. And `data['main_content'] = $contenttofill`. you just call it in your controller – leonardeveloper Dec 12 '13 at 22:07
  • Ok I understand that, and how do I actually load the view from the view? Do I use include or require? And thanks for the tutorials, going to watch them all. – Orel Bitton. Dec 12 '13 at 22:11
  • Please check the second line of code in my answer wherein `template.php` loaded(included) the `header` and the `footer` in using `$this->load->view('templates/header')` and `$this->load->view('templates/footer')` – leonardeveloper Dec 12 '13 at 22:15