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 8045553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:35:08+00:00 2026-06-05T05:35:08+00:00

I have a set of controllers that should only be accessible if you are

  • 0

I have a set of controllers that should only be accessible if you are an admin (as opposed to a regular user).

Thus, in the constructor for each of the controllers, I would do:

public function __construct() {
    parent::__construct();
    if (! is_admin()) {
         show_404();
    }
}

Instead of adding this code to the constructor of every Admin Controller, is there a better way to do this?

I was thinking I could create a Base controller called Admin_Controller that would look like this:

public class Admin_Controller extends CI_Controller {

    public function __construct() {
         //the above code goes here
    }
}

And then all my other controllers can extend this class, instead of the CI_Controller class. The only problem with this is, I need to include this file at the top of my other controllers, or CodeIgniter cannot find Admin_Controller.

Is there a better way to do this?

  • 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-06-05T05:35:10+00:00Added an answer on June 5, 2026 at 5:35 am

    place this in your application/core folder: MY_Controller.php (note the correct use of capitals)

    class MY_Controller extends CI_Controller
    {
    public function __construct() 
    {
         parent::__construct();
         if (! is_admin())
         {
              show_404();
         }
    } 
    

    Then in all your normal controllers that you want users to be logged in

    class Whatever extends MY_Controller
    {
       public function __construct() 
        {
             parent::__construct();
        } 
    }
    

    See userguide here on extending core classes

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

Sidebar

Related Questions

I have a situation in one of my controllers that should only be accessed
I have a set of Controller/View that are composed dynamically, each one have a
I have a view controller that has a tableView in it. When I set
I have a horribly coded set of controllers which I am unable to refactor
I have set the eclipse java formatter to wrap lines that exceed 120 characters
I have set of flat files (114 files) each file is named with database
It should be simple, but it's not. I have a library that is hoping
Several of my controller actions have a standard set of failure-handling behavior. In general,
I have a very complex controller with a very complex set of views and
I have set a background on the data-role=page element like so <div data-role=page style=background:

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.