Rohan Kumar
Rohan Kumar

Reputation: 40639

Coding in Controller and Model in Codeigniter

I am new in codeigniter and I am little bit confused about code written in Controllers and Models in Codeigniter

Best way to write code for logout

Controller Logout.php

<?php 
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Logout extends CI_Controller {

        public function __construct()
        {
            parent::__construct();
            $this->load->model('logout_model');
        }

        function index()
        {
            $this->logout_model->logout();
        }
    }
?>

Model Logout_model.php

<?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Logout_model extends CI_Model {
        public function __construct()
        {
            parent::__construct();
        }
        function logout()
        {
            $this->session->userdata = array();
            $this->session->sess_destroy();
            redirect(base_url().'admin/login','refresh');
        }
    }
?>

Also all our logic not related to database should be written in Model or Controller Is it correct, that only the redirections are handled by Controllers?

Same thing if I will do in Cake php then what are the changes in that case?

Upvotes: 1

Views: 3716

Answers (5)

Rohan Kumar
Rohan Kumar

Reputation: 40639

After searching, I got some useful URLs for this,

  1. http://www.codinghorror.com/blog/2008/05/understanding-model-view-controller.html
  2. http://pic.dhe.ibm.com/infocenter/wchelp/v6r0m0/index.jsp?topic=%2Fcom.ibm.commerce.developer.doc%2Fconcepts%2Fcsdmvcdespat.htm
  3. http://framework.zend.com/manual/1.12/en/learning.quickstart.intro.html
  4. http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model

Model - This is the part of your application that defines its basic functionality behind a set of abstractions. Data access routines and some business logic can be defined in the model.

View - Views define exactly what is presented to the user. Usually controllers pass data to each view to render in some format. Views will often collect data from the user, as well. This is where you're likely to find HTML markup in your MVC applications.

Controller - Controllers bind the whole pattern together. They manipulate models, decide which view to display based on the user's request and other factors, pass along the data that each view will need, or hand off control to another controller entirely. Most MVC experts recommend » keeping controllers as skinny as possible.

Upvotes: 0

JamesN
JamesN

Reputation: 387

Follow CodeIgniter document :

  • The Model represents your data structures. Typically your model classes will contain functions that help you retrieve, insert, and update information in your database.
  • The View is the information that is being presented to a user. A View will normally be a web page, but in CodeIgniter, a view can also be a page fragment like a header or footer. It can also be an RSS page, or any other type of "page".
  • The Controller serves as an intermediary between the Model, the View, and any other resources needed to process the HTTP request and generate a web page.

So I agree with @deceze that: CakePHP, Codeigniter and many similar frameworks do not do MVC correctly.

So if you're familiar with some java framework like hibernate or jpa, you can easier make model of CodeIgniter's model work as ORM system by using a third library like CI datamapper .. It make you model work as a base or database object system where you just care about the nature db object that your system is working with.

Upvotes: 0

J&#233;r&#244;me
J&#233;r&#244;me

Reputation: 2090

In CodeIgniter, models are used to interact with the database. Personally, I would go for a more pragmatic approach and leave out the logout model in your case :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Logout extends CI_Controller {

    function index()
    {
        $this->session->sess_destroy();
        redirect('admin/login','refresh');
    }

}

// Omit PHP closing tag to avoid outputting unwanted data

CodeIgniter Style guide : PHP closing tag

Upvotes: 1

deceze
deceze

Reputation: 522015

CakePHP, Codeigniter and many similar frameworks do not do MVC correctly.

The "model" is "your app". Everything your app does belongs in the model. This includes sessions, log in, log out, authentication etc.
The view contains code exclusively related to presenting and visualizing data from the model.
The controller is a little bit of glue that holds both parts together, in a web application it mostly deals with receiving input and invoking the correct model method and view.

In short: The view is interchangeable depending on what output you need, the controller is interchangeable depending on what input you get, the model is not interchangeable because it is your core app. Once you understand that, a proper MVC structure should be obvious.

Cake and CI put waaaaaay too much logic into the controller and keep the models waaaaay too thin, to the extend that it's impossible to make a proper MVC app with their default controllers and models. And by "thin models" I mean that they typically only have one type of "model", which is a DAL class. The "model" in MVC is not just one type of class, it is a group of things in whatever shape necessary to model your business logic.

So, whatever you do, know that it's not proper MVC. Be aware of the ideal separation described above and that you're never going to attain it using these frameworks.

Upvotes: 2

user2401931
user2401931

Reputation:

Code Igniter follows MVC pattern. That means Model are database layer, Views are front end and Controllers are intermediary layer between models and views. The basic flow of CI is like this.

  1. User makes a request (controller's action called)
  2. Controller looks if there is some thing to do with the database layer (and performs the functionality)
  3. Controller then also sends back the data to the view with required data.

While using CI, we have to follow the MVC rules as we should not echo any thing in controllers and models. So there is no need to write the logic in models that is not related to database.

Upvotes: 0

Related Questions