getting rid of my duplicate code! calling the same model functions every view |
[eluser]fill0000[/eluser]
I decided to build a small in-house system running on the intranet for a roofing company that needs to seamlessly track enquiries from administration through to the estimation team, then through to the accountant, etc - all in all, it works great and everyone happy - but I haven't exactly understood some areas of codeigniter (having no previous know-how of MVC). Basically, I have a side menu view which counts the number of enquiries that are currently waiting to have an estimate and number of estimates waiting to be sent out to the customer, etc. gd_roofing.php (The Controller) Code: // Show customer page gd_roofing_model.php (The Model) Code: // Count the total number of customers without an estimate The side menu view example inside an UL Code: <?php echo 'Estimates Required '.'( '.$estimates_required_counter.' )'; ?> As you can see it's quite a bit of hassle having to repeat this code for every view and very badly coded, but I'm not sure how to rectify this in the best way. My views load in a header and footer and side menu views manually - the entire system follows this structure. I'm very grateful for any input
[eluser]fill0000[/eluser]
Otemu, Firstly, thanks for your input on my question, Do you recommend an overhaul in the way I'm templating the website? This is obviously quite a big task at this point, which I'm prepared to do over this weekend but is this primarily why I'm in the situation of having to load all these models per view, is there no global way to just load them? I want to stay between the lines when it comes to satisfactory coding, although a complete overhaul may cause me time issues! Thoughts? Cheers Phil
[eluser]Otemu[/eluser]
Hi, Well number away of ways to tackles I recommend you do this which shouldn't take you too long hopefully: 1. install codeigniter-modular-extensions-hmvc - most of the work will probably involve you setting this up as you need a clean install of Codeigniter 2. Now you have that installed create a module and load it in one of your views 3. Create a module for your side menu, controller model view 4. Now remove all your repeated code from the controller 5. Now in every view you have the side bar just load the module Code: <?php echo Modules::run('module/controller/method', $param, $...); ?> Hope that helps.
[eluser]fill0000[/eluser]
Otemu, Many thanks for your good suggestion, I'll try this tonight! Kind Regards Phil
[eluser]boltsabre[/eluser]
If I follow this correctly, you're just concerned about displaying a few bits of information in your LHS "navigation" column. For a small internal website/intranet site, I think that hmvc is a bit of an overkill (but it's still a great idea to know about it!), wouldn't simply extending the controller with MY_Controller be a simpler, more time efficient solution? Simply have a MY_Controller which extends your base CI_Controller, have the MY_Controller do everything you need, then load the results into the global data array. Code: $data['estimates_required_counter'] = $this->gd_roofing_model->estimates_required_counter(); Then if you have all your controllers extend MY_Controller, instead of CI_Controller (simple notepad++ search/replace) and presto, you have you data available across the site!
[eluser]fill0000[/eluser]
Thanks so much, both of you. Great answers, I've learnt some valuable new stuff! In this project I'll use boltsabre's approach simply because it takes a vast amount of complexity out of this small system, I was able to do it in 10 minutes. In my future bigger projects I'll consider going down the route you mentioned Otemu, I wish I knew about that about 2 weeks ago! Many thanks Phil
[eluser]boltsabre[/eluser]
Happy we could help and glad you got it up and going so quickly, I love the power of MY_Controller, it's great for sitewide stuff like this!!! Just a word of advice for the future, if you use HMVC it will break your "callback" validation functions (if you have any) without a little work-around - so keep an eye out for it once you start getting dirty with HMVC. Otemu just saved my bacon today with this problem!!! |
Welcome Guest, Not a member yet? Register Sign In |