Welcome Guest, Not a member yet? Register   Sign In
Blog Tutorial .... Separating Model from Controller ?
#1

[eluser]Jason Curran[/eluser]
Hello all! My first post here ..

I'm admittedly new to MVC and CodeIgniter ..

I've gone through the video tutorials, which are excellent, and they are the reason I'm choosing to learn MVC by way of CI ..

My question is .. wouldn't it have made sense to separate the Model from the Controller in the tutorial?

I'm trying to expand on the sample blog application, but I'm getting stuck at creating a Model from which my Controller can pass on the information to the View ...

So far I am having trouble finding even ONE complete, extremely simple, sample CI application that makes use of all three elements of the MVC pattern.

I did check out the excellent tutorial from http://video.derekallard.com/ but that is a bit too advanced and far off from the blog tutorial ..

Can anyone point me in the right direction or even better: help me break down the Controller from the blog video tutorial into a Model and Controller?

My Controller code from the blog tutorial is below.

Many Thanks! JC

Code:
<?php

class Blog extends Controller {
    
        function Blog()
        {
            parent::Controller();    
            
            $this->load->helper('url');
            $this->load->helper('form');
            
        }
        
        function index()
        {
            $data['title'] = "My Blog Title";
            $data['heading'] = "Blog Heading";
            $data['query'] = $this->db->get('entries');
            
            $this->load->view('blog_view' , $data);

        }        
        
        function comments()
        {
            $data['title'] = "My Comment Title";
            $data['heading'] = "My Coment Heading";
            
            $this->db->where('entry_id', $this->uri->segment(3));
            
            $data['query'] = $this->db->get('comments');
            
            $this->load->view('comment_view' , $data);
        }        
        
        function comment_insert()
        {            
            $this->db->insert('comments' , $_POST);
            
            redirect('blog/comments/'.$_POST['entry_id']);
        }
}


?>
#2

[eluser]wabu[/eluser]
Here's a simple CRUD example which might help:

http://henrihnr.wordpress.com/2009/04/26...plication/

I think the CI user guide is very good but it can be hard to follow the tutorial thread in the midst of the reference. Something like the above would be very helpful (maybe it could be made a sticky in the forum here).
#3

[eluser]jedd[/eluser]
Hi Jason and welcome to the CI forums.

Just think of the model as being all the DB stuff, and the Controller as everything else. That'll get you a little way further (it's obviously more complex than that).

Here's a very quick separation of the code you've shown, as split up into a model and a controller.

Note this is very much not tested so probably some silly typos in there.

And I hate the CI way of indenting, so you'll have to cope with my formatting.

Oh, and of course this is not how you'd do it In Real Life - you'd not pass query results around, let alone pass them everywhere (M, V and C) - instead you'd send off the parameters to the model, the model would return a lump of data (array, object, arrays of either) then pass that over to the view.

Controller:
Code:
<?php

class Blog extends Controller {
    
    function Blog()  {
        parent::Controller();    
            
        $this->load->helper('url');
        $this->load->helper('form');
    
        $this->load->model('Blodel');
        }


    function index()  {
        $data['title'] = "My Blog Title";
        $data['heading'] = "Blog Heading";

        $data['query'] = $this->Blodel->get_entries()

        $this->load->view('blog_view' , $data);
        }        


    function comments()  {
        $data['title'] = "My Comment Title";
        $data['heading'] = "My Coment Heading";

        $data['query'] = $this->Blodel->get_comments($this->uri->segment(3));
            
        $this->load->view('comment_view' , $data);
        }        


    function comment_insert()  {
        $this->Blodel->insert_comment ($_POST);
        redirect('blog/comments/'.$_POST['entry_id']);
        }

    }



Model:
Code:
<?php

class Blodel extends Model {
    
    function Blodel ()   {
        parent::Model();    
        }

    function  get_entries()  {
        return $this->db->get('entries');
        }

    function  get_comments  ($entry_id)  {
        $this->db->where('entry_id', $entry_id);
        return $data['query'] = $this->db->get('comments');
        }

    function  insert_comment  ($post_data)  {
        $this->db->insert('comments' , $post_data);
        }

    }
#4

[eluser]Jason Curran[/eluser]
WOW ... I really appreciate the replies!

Both are extremely helpful ..

Of course, I still welcome any additional input others can provide

Thank you!
#5

[eluser]Jason Curran[/eluser]
Quote:Oh, and of course this is not how you’d do it In Real Life - you’d not pass query results around, let alone pass them everywhere (M, V and C) - instead you’d send off the parameters to the model, the model would return a lump of data (array, object, arrays of either) then pass that over to the view.

This is probably one of the most frustrating aspects of learning new frameworks .. it's very difficult to find "Real Life" examples..

I'm not picking on CI specifically .. I think they have put forth the most effort of any PHP Framework to make it easy for n00bs ... but I just don't understand why these people, who are smart enough to make an application framework in the first place, would create a framework without providing a few "Real World" sample applications ..

I think it would go a long way toward getting people to adopt the framework ..
#6

[eluser]Dam1an[/eluser]
[quote author="Jason Curran" date="1250648402"]
This is probably one of the most frustrating aspects of learning new frameworks .. it's very difficult to find "Real Life" examples..[/quote]

Check out bamboo invoice made by the might Derek himself
That should give you a nice 'real world' example of an app built in CI
#7

[eluser]jedd[/eluser]
[quote author="Jason Curran" date="1250648402"] ... create a framework without providing a few "Real World" sample applications ..[/quote]

Stick with it,Jason .. like most complex things, there's several 'Aha!' moments that you'll have when a bunch of things all suddenly make sense all of a sudden.

I think with 'real world' examples, well, there's bamboo invoice, and it'd be hard to find anything realer worlder than that.

With very small, trivial applications, it's trickier - because in some ways it's more complex to use a framework to develop a very small, trivial application - so it's not a compelling case. I suspect most people come to a framework after working with raw PHP, perhaps with some modular practices, but certainly some exposure to non-trivial applications. For them it's a much easier sell - the benefits are much more obvious, and also the way of separating the functionality makes much more sense (convergent evolution - most programmers would eventually head towards this kind of layout anyway).

In any case, there's certainly plenty of tutorials - several excellent screencasts, lots of blogs, and a plethora of example code floating about in the forums.




Theme © iAndrew 2016 - Forum software by © MyBB