• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Application structure question

Hey guys,

I have a question that's been bothering me for days. My application runs on 2 templates, one for homepage and one for frontpage.

Now, for example, if I add a shared component to the sidebar, some data being pulled from the database, I will have to update every single controller and every single function in those controllers to pull the data and pass it to the template.

Please let me know if the explanation above is unclear.

I'd love to hear what you guys think will be a nice way to tackle this problem or restructure my application.

Thanks in advance.

[eluser]Michael Wales[/eluser]
You could take the Modular approach with Modular Extensions or Matchbox (or even create a Sidebar controller and call it using Wick).

Or, you could make a class that extends Controller - place all of your code shared across controllers in it - then extend your normal Controller class off of this one.

class BaseController extends Controller {
  function __construct() {
    $this->data->sidebar = $this->sidebar->get();

class Users extends BaseController {

[eluser]Colin Williams[/eluser]
Look into writing hooks or extending the CI_Controller class (or maybe even Output class instead).

People run into this block ALL THE TIME it seems, and everyone seems to want to start re-inventing or skirting the MVC structure (which could be valid is some cases). I think what happens is that people just cook up a $data array in every controller and pass it on to the view, like in the examples, but this system runs into limitations pretty quickly, as you've recently discovered.

Certain components of your application should be able to hook into the final output regardless of what Controller is instantiated, but each Controller should still have final say about what makes it through. Taking the Matchbox/ModularExtensions/Wick approach means your Views handle this burden. It's up to you whether or not you agree with this behavior. I have a library that facilitates the former behavior, and is even kinda geared to what your talking about: You have one or several main templates that may or may not share certain regions. PM if you're interested. Anybody! I will be releasing it into the community soon, but I want to properly package and document it first.

Thanks for your replies guys. Ill look into your suggestions.

Here's my temporary workaround for the "shared" components.

My application runs on 2 templates - home and inner.

To add that piece of shared component on the right hand menu (across all pages) and not having to updates all controllers and functions, I added an ajax call that loads the some URL into an empty DIV.

e.g url: site.com/myNewController/SomeFunction

where someFunction grabs whatever info I need from the database and passes the info to a view file to format the data the way I want.

I hope this helps if someone is looking for a non-ordinary solution.

[eluser]Colin Williams[/eluser]
Quote:I added an ajax call that loads the some URL into an empty DIV.

2 cheers for accessibility! Sheesh... You'd be much better off with something like wiredesignz's Modular Extensions. You could just call a controller straight from your home and inner views like <?= modules::run('module', $data, 'method') ?> . Is it the best solution? Well, we've debated that... It's certainly the best available, IMO. Much better than relying on the client to load it. Much, much better.

[quote author="Colin Williams" date="1216809540"]2 cheers for accessibility! Sheesh...[/quote]

I second the sentiment. Also, how accessible is it for you? 6 repetitive controllers or a half dozen Ajax requests?

I don't think you'd have to go too far astray from base CI to get what you need. Libraries can act as a portable MVC, if you will. Just instantiate the CI super object
$this->CI =& get_instance();
from within your library. From there, you can load models, helpers, other libraries, and views with
. Have your library's methods return views, and you will keep the impact on your controllers down to one or two lines.


A library loading views?... This sounds suspiciously like a controller to me.

[eluser]Colin Williams[/eluser]
Quote:A library loading views?… This sounds suspiciously like a controller to me.

I sort of agree. It's not something I would do. However, consider the alternatives for separating markup from the library: includes, embedded in PHP? No thanks.

For this exact situation, I would use Michael Wales' described method of extending the Controller class.

[quote author="Colin Williams" date="1216820882"]I sort of agree.[/quote]

You are allowed to agree with some things Colin. :lol:

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.