CodeIgniter Forums
How to best organize module-determined content, which will be different for users - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: How to best organize module-determined content, which will be different for users (/showthread.php?tid=74970)



How to best organize module-determined content, which will be different for users - bmcn - 12-02-2019

So we're adding paid modules to our site and I'm really not sure what the best way to implement this would be. The modules wouldn't really work as their own controllers since the content will be handled largely by existing controllers with minor changes to content based on if the module is active or not.

I think I see a few ways to go about it:

  1. Minimal use of separate files/class
    +This would involve basically using module access as an if/else statement which would work fine for simpler things like showing a single element or not
    +This would also work well for completely separated content based on access or not, you could call the appropriate function which would handle things differently
    -This would make complex functions that are largely the same with minor differences rather messy I'm thinking. There would be many if else checks weaved into the functions or repeated code is the if else blocks are larger.

  2. Mostly handle things in separate functions, and put as much common code, even if it's like 90%, into smaller functions both methods(active/not active) will call
    +Definitely the easiest to read, as both functions will be calling descriptive function names in between the code that is not common
    -Making changes that get carried through would be tedious since you'd need to alter so many functions?

  3. Move most of the module-dependent code into 'Module libraries', called through $this->supportmodule->showFooterHelpOptions()
    +Clear distinction between code that is universal and code that is dependent on a module being loaded or not
    -Breaks the standard M-V-C convention? The library function responsible for showing the content could still load different views, but would be taking the role of the controller on more complex tasks.
I guess I'm leaning towards #2. I haven't really made anything like this with an MVC setup that wasn't completely separated, where you could access the area or you couldn't, so organization was easy. With things being more interwoven here I'm feeling a little lost.

Any advice would be appreciated, thanks,


RE: How to best organize module-determined content, which will be different for users - jreklund - 12-03-2019

Depends on what the paid module are supposed to do. In case you need to have a separate Controller to handle the modules action, or a common controller can be used.

But for renderingen them (boxes?) I would build a base module with common functionality like render() and extend it from all modules, so you can insert a new box anywhere on the site. You can make it so that you always include it, and in the module itself determine if it should be rendered or not. For this to work you need to pass along what type of modules the user have paid for.


RE: How to best organize module-determined content, which will be different for users - bmcn - 12-03-2019

That sounds good for the boxes. One of the modules we'll be doing is to allow the owner to use their own payment gateway(s) instead of ours, that one would definitely be integrated into already existing controllers, I guess I had that in mind when I was thinking about where to put the module-only code. Right now we offer Paypal only but if the owner has Stripe then we'll be supporting that too. I guess since that wouldn't be common to all modules it would then belong in the payment-handling controller after all, and not in a place dedicated to modules. Maybe I just needed to talk/type this out, thanks.


RE: How to best organize module-determined content, which will be different for users - maxxd - 12-03-2019

I'd point towards option 2, but I'm a bit confused about your con on the idea. The only time you have to change many functions using the method described is if you change function names. You could go with if/else conditionals dependent on the access level or you could create a master function object with the common functionality, then create child objects named after the access level of the current user and include those by default. For instance, something along the lines of the following:



class BaseFunctionality extends CI_Controller{
  public function showSection(){
    $this->showTheStuff();
  }
}

class PaidFunctionality extends BaseFunctionality{
  public function showSection(){
    $this->dontShowThat();
  }
}

class PublicFunctionController extends CI_Controller{
  private $_access = "Base";
  public function __construct(){
    if(is_logged_in()){
      $this->_access = "Paid";
    }
  }
  public function do_something_cool(){
    $fn = "{$this->_access}Functionality";
    if(file_exists($this->$fn) && is_executable($this->$fn)){
      $this->$fn();
    }
  }
}