Welcome Guest, Not a member yet? Register   Sign In
Question about the MVC pattern
#11

[eluser]wiredesignz[/eluser]
It does seem to break the rules doing it that way... but if no-one is looking, who cares. :lol:

CodeIgniter provides access to the model inside the view, So I guess there are no REAL rules here.
#12

[eluser]Randy Casburn[/eluser]
He likely needs help thinking through his design rather than his fetch() statement. Do you think? If he's got 500 controllers there might be a different kind of issue. If he really, really needs 500 controllers he needs to write a Controller Interface and get it done once. But these are design issues, not fetch() issues.

IMHO,

Randy
#13

[eluser]loathsome[/eluser]
The application have, as per today, about 10 controllers, and right now every single one of them looks like this:

Code:
class VerifyId extends Controller {

function index()
{
   $data['menu'] = $this->menu_model->fetch();
   $this->load->view('verified/verifyid', $data);
}
function someotherFunction()
{
   $data['menu'] = $this->menu_model->fetch();
   $this->load->view('etc/someotherFunction', $data);
}
  // et cetera, et cetera
}

It'd be so much simpler to just FORGET about fetching the menu, and let the model do it's job directly from the view file. Don't you agree?

How does other applications do it, when having for example a sidebar? I really fail to see how my method is "wrong", seeing as it is much, much easier to apply. Still, I want to do this the right way.

@randy Casburn: What do you mean by a «controller interface»? Many thanks.
#14

[eluser]Colin Williams[/eluser]
I agree with Randy. It's a design issue, not an MVC issue.
#15

[eluser]loathsome[/eluser]
So what do you recommend I do, then?
#16

[eluser]Colin Williams[/eluser]
What exactly are you doing that makes you think you need fetch the results from the view? We're not in tune with your app, so we can't offer much more advice.
#17

[eluser]Steve Grant[/eluser]
[quote author="loathsome" date="1215852071"]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.[/quote]
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
Code:
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
Code:
class Start extends MY_Controller
{
    function __construct()
    {
        parent::MY_Controller();
    }
}
#18

[eluser]Randy Casburn[/eluser]
@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
#19

[eluser]loathsome[/eluser]
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!

Quote: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!
#20

[eluser]wiredesignz[/eluser]
@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.




Theme © iAndrew 2016 - Forum software by © MyBB