Part of the EllisLab Network
   
2 of 2
2
Question about the MVC pattern
Posted: 12 July 2008 09:13 AM   [ Ignore ]   [ # 16 ]  
Summer Student
Total Posts:  20
Joined  03-17-2008
loathsome - 12 July 2008 03:41 AM

I am autoloading my model now, and accessing it directly in the view file. I find this a lot easier than initializing the model in EVERY single controller.

An alternative would be to extend the Controller class (e.g. MY_Controller), and place the call to the repeating function(s) in the constructor for MY_Controller. Then of course any controller that uses that code will need to extend MY_Controller instead of simply Controller.

That way, you maintain the MVC pattern a little more strictly.

Example:

system/application/libraries/MY_Controller.php

class MY_Controller extends Controller
{
    
function MY_Controller()
    
{
        parent
::CI_Base();
        
$this->_ci_initialize();
        
log_message('debug''MY_Controller Class Initialised');

        
// now put all the code you want executed within every controller here
    
}

system/application/controllers/start.php

class Start extends MY_Controller
{
    
function __construct()
    
{
        parent
::MY_Controller();
    
}
Profile
 
 
Posted: 12 July 2008 10:43 AM   [ Ignore ]   [ # 17 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

@loathsome—in response to your earlier question about “what is an Interface”, Steve just provided the way to build a one-to-many controller class for CI. It is a good option if your design calls for this solution.

I still think that rather than hacking on it here and there, you should think about:

1) WHY is code is being repeated vertabim in different places
2) HOW you can consolidate the repeated code (maybe Steve’s solution - maybe others)
3) SHOULD you consolidate the repeated code (maybe it’s a bad idea)
etc…

You already know the maintenance headache you’re in for.  So fix it now with a proper design. Steve may have offered the best choice…maybe not.  But since no one here but you knows what you’re trying to accomplish, no one here is really prepared to help you.

Randy

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
Posted: 12 July 2008 11:09 AM   [ Ignore ]   [ # 18 ]  
Grad Student
Avatar
Rank
Total Posts:  78
Joined  07-10-2008

Superb, thanks a bunch for the very helpful and informative replies.

I can tell you one of the things we’re doing—on every page, there will be shown an information/“Status” box. This fetches data from a MySQL-table (but only every 10 minutes to save load). This box is shown everywhere, except on the about/ page. When using the model directly in the view, we had a if($!hide_status){ $this->model->blabla } statement, so we could easily hide the box by passing $data[‘hide_status’] = true to the view page. Right now we’re seriously considering Steve’s solution, and it looks like we’re going with his.

Please feel free to tell me exactly how you would solve this!

You already know the maintenance headache you’re in for.  So fix it now with a proper design. Steve may have offered the best choice…maybe not.

Exactly, that’s why I’m asking so many questions. I want to get everything straight from the beginning!

Thanks again!

Profile
 
 
Posted: 12 July 2008 11:30 AM   [ Ignore ]   [ # 19 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007

@Steve, Could you explain why you are calling CI_Base constructor from MY_Controller, while it extends the Controller class?

Also why you call the private method _ci_initialize()?

And Welcome to the forums too.

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 12 July 2008 11:39 AM   [ Ignore ]   [ # 20 ]  
Summer Student
Total Posts:  20
Joined  03-17-2008
wiredesignz - 12 July 2008 03:30 PM

@Steve, Could you explain why you are calling CI_Base constructor from MY_Controller, while it extends the Controller class?

Also why you call the private method _ci_initialize()?

Certainly can… I simply copy and pasted from the Controller class constructor wink

And Welcome to the forums too.

Thanks, I’ve been reading the forums for some time, having been using CI for nearly a year, but now feel confident enough to answer questions posed by others :D

Profile
 
 
Posted: 12 July 2008 11:48 AM   [ Ignore ]   [ # 21 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007

I think the correct definition for the MY_Controller extension is:

class MY_Controller extends Controller
{
    
function MY_Controller()
    
{
        parent
::Controller();
    
}

Just so we don’t confuse people.
All other class extensions follow the same pattern.

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 12 July 2008 12:29 PM   [ Ignore ]   [ # 22 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007
loathsome - 12 July 2008 03:09 PM

Superb, thanks a bunch for the very helpful and informative replies.

Please feel free to tell me exactly how you would solve this!

You’re operating on the assumption that the pronoun “this” is easily extrapolated since you already know the whole of “this”.  Therefore, everyone else must intrinsically know what “this” is through osmosis. 

Forgive me if I sound like a fool if I cannot give a solution for [ exactly how I would solve “this” ].

You’ll get lots of answers from folks in this forum based on the presumptive information you’ve provided. Some more depth would be helpful.

Randy

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
Posted: 12 July 2008 01:04 PM   [ Ignore ]   [ # 23 ]  
Grad Student
Avatar
Rank
Total Posts:  78
Joined  07-10-2008

Isn’t this explained detailed enough?

I can tell you one of the things we’re doing—on every page, there will be shown an information/“Status” box. This fetches data from a MySQL-table (but only every 10 minutes to save load). This box is shown everywhere, except on the about/ page. When using the model directly in the view, we had a if($!hide_status){ $this->model->blabla } statement, so we could easily hide the box by passing $data[’hide_status’] = true to the view page. Right now we’re seriously considering Steve’s solution, and it looks like we’re going with his.

If not, I’m not sure what more I could say. Please advise me.

Profile
 
 
Posted: 12 July 2008 03:07 PM   [ Ignore ]   [ # 24 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

@loathsome—rather than me brandishing the sword blind folded, why don’t I just tell you how I build my castles wink  [ just trying to bring some humor to this ]

Here is the way I solve this type of…

I have this status thingy that updates every so often on every page and yada yada yada…

That doesn’t mean it is the only way, the best, way, or even a good way.

I use AJAX extensively.  I use AJAX extensively because of things like this.  Asynchronous calls back to specialized controllers that handle security specifically designed for AJAX requests, pull data directly from models designed specifically for formulating data as JSON responses.  I made a conscience DESIGN decision to break the MVC chain (please forgive me wirebydesignz) by having my JSON responses echo directly out of my models back to my calling pages. 

Here is a post that sort of lays out the sequence http://codeigniter.com/forums/viewreply/423884/

If you don’t know what AJAX is or don’t know what JSON is or what asynchronous page updates are there are lots of posts in these forums to help.

Now if you start to think about designing your system with these types of capabilities, you might think about sending “server load” indicators with your JSON data.  That might lead to you applying the ability of adjusting the call-to-server timing based upon the “server load” characteristics.  So now you are strapped to “10 minute” updates, but maybe the status is now one of those “sliding window” things you studied about in college (maybe last week or last year or in 1990 or not ever smile ).  You can adjust it on-the-fly.

I’m not saying this is good, bad, right, wrong, better, worse, up or down.  I’m just saying this is how I solve similar dillemmas.  Maybe you’ll find this more helpful than my previous posts.

Randy

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
Posted: 12 July 2008 08:51 PM   [ Ignore ]   [ # 25 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007

@loathsome, You seem to have reached a point where Modular Extensions - HMVC would provide a solution for you.

While Randy uses Ajax calls to render his partial by calling a controller from his view remotely, ME allows you to create a module that you can call to build partial views such as your status box at server level.

<?php echo modules::run('status'$data'render'); ?> 

@Randy, I forgive you. But I don’t recommend HTML output from models, you could just as easily load and return a view. wink

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 12 July 2008 10:20 PM   [ Ignore ]   [ # 26 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

@wired—promote promote promote - I swear your going commercial any day now.  Just like ExtJS! hehehhehehe Get everyone on board and…

Anyway -

@Randy, I forgive you. But I don’t recommend HTML output from models, you could just as easily load and return a view. wink

I know you don’t. That’s why I said what I did. It is also a carefully crafted design decision, knowing in advance that I am bending the rules.  Unlike most, I’m fairly careful about keeping things straight.  I’ll demonstrate in a minute. But first, the design thingy just for fun…

Why on the green green pastures of Antarctica would I want to drive to Noviscotia in order to get to Brazil?  I have no need for the output class, nor all the calls along the way that it takes to get output that CI won’t format for me along the anyway (search JSON in the UG).  So your postulation would have me create a JSON encoded string that CI, in all of it’s graces, would hand from one object to the next through the object hierarchy until getting dumped back to the output object where it will get sent to the browser—my objective in the first place.  So what did CI do with it? Nothing.

Now back to the “I’m careful about design stuff.”  The sequence diagrams are too large to upload into the forums space, but this model should give you an idea of how I keep track of design issues with CI and other platforms.

Randy

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
   
2 of 2
2