Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For CodeIgniter one way of going about abstracting the logic is to create a super class as such:</p> <p> <pre><code>class MY_Controller extends CI_Controller { public function __construct() { parent::__construct(); //...do stuff... } //...do stuff... } /* End of file MY_Controller.php */ /* Location: ./application/core/MY_Controller.php */ </code></pre> <p>All your controllers that you want to share the same logic should then extend MY_Controller instead of CI_Controller</p> <p>Now in your MY_Controller you can create functions to "handle incorrect parameters across multiple methods".</p> <p><strong>UPDATE</strong></p> <p>Your controller function params are coming from your route. e.g. <code>mydomain.com/mycontroller/myfunction/param1/param2/</code> would go to the <code>mycontroller</code> <code>public function myfunction($param1, $param2)</code></p> <p><code>$site_id</code> is just your variable naming. You can't tell what it's named and even if you could i doubt it's a good idea to based any logic on how you name your variables.</p> <p>A simple way is just to explicitly call the validating function (stored once in your base controller, a library or a helper) in each controller's function that require it, that gives you fine grain control.</p> <p><strong>UPDATE</strong></p> <pre><code>Extending the controller would only work if my parameters were the same for each method. I'd like to detect automatically the parameters and perform checks. I suppose a call to a helper might be the best I can do, but Id just like to be able to completely abstract it away from the method. I suppose one option would be to have an array of which controllers and methods have $site_id as their first parameter and have a pre_controller hook to do the checking. But this would mean keeping the array up-to-date which kind of defeats the object of my question which is to make things as DRY as possible! </code></pre> <p>Generally, to gain automation you must give up flexibly and follow conventions. so to not go the "use helper" way and to go the auto detect way... create your own convention.</p> <p>in your base controller use $this->uri->segment(n) to check if a certain URL pattern exist, if it does you know that the site_id param is incoming and that you can run automatic validation on it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload