Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7509131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:48:59+00:00 2026-05-29T22:48:59+00:00

I’m codeing in codeigintier but this question equally applies to any MVC framework. How

  • 0

I’m codeing in codeigintier but this question equally applies to any MVC framework.

How do I abstract the logic for determining if a parameter for a controller method is valid across mutlple methods that take some of the same parameters?

EDIT: More detail

For example in the application I’m building at the moment there are multiple ‘sites’ and nearly all the data in different tables belongs to a site and a user can only view a subset of the sites. So lots of my methods start method($site_id,...) so it would be really nice if I was able to have a pre_controller hook which could look at the parameters for the methods and detect if there was a $site_id in the parameters, and if there was check the permissions.

The problem is that not all the methods in those controllers will all have a $site_id parameter, so I cannot do the checking automatically in the constructor of my extension of CI_Controller

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:49:00+00:00Added an answer on May 29, 2026 at 10:49 pm

    For CodeIgniter one way of going about abstracting the logic is to create a super class as such:

    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 */
    

    All your controllers that you want to share the same logic should then extend MY_Controller instead of CI_Controller

    Now in your MY_Controller you can create functions to “handle incorrect parameters across multiple methods”.

    UPDATE

    Your controller function params are coming from your route. e.g. mydomain.com/mycontroller/myfunction/param1/param2/ would go to the mycontroller public function myfunction($param1, $param2)

    $site_id 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.

    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.

    UPDATE

    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!
    

    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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.