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

  • Home
  • SEARCH
  • 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 8897305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:18:23+00:00 2026-06-15T00:18:23+00:00

I have about 9 lines of code that will be the same in many

  • 0

I have about 9 lines of code that will be the same in many of my controllers. They go in the index and are used to check whether the user is logged in, what level of access has been granted, and grab some session variables. What’s the best way to include snippets of code that are not complete functions within themselves?

The snippets are not in all controllers, just the ones that build the admin section of the application.

It would be great if I could use one line of code to include the snippet but I’m new to CodeIgniter and I don’t want to stray from best practices. If you could include a brief example that would help me visualize this. Thanks!

  • 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-15T00:18:25+00:00Added an answer on June 15, 2026 at 12:18 am

    You want to create an extension of the base CI controller and do your checks there. Extending the cores are in the documentation here: http://ellislab.com/codeigniter/user_guide/general/creating_libraries.html Scroll down to the Extending Native Libraries part.

    In your new controller you can do something like this:

    <?PHP
        class MY_Controller extends CI_Controller
        {
    
       function __construct()
       {
        parent::__construct();  //Gets all the CI_Controller functions and makes them available to this controller.
        if($this->session->userdata('is_logged_in')
        {
                $this->load->model('categories');
        $this->categories=$this->categories->getAllCategories();
        }
    
           }
         }
    

    Then in your other controllers you use MY_Controller instead of CI_Controller like this:

    <?php
    
    class Pages extends MY_Controller {
    

    Obviously you’ll have your own checks and variables to load but this should give you the general idea. In the example above I can now use $this->categories to retrieve my categories anywhere in the application that’s been loaded with MY_Controller.

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

Sidebar

Related Questions

In the example I'm thinking of I have about 4 lines of code that
I have about 2000 lines of javascript code for my application. A lot of
I have a file of about 19500+ lines of code. I am doing work
I have been wondering about the following lines of code [self performSelector:@selector(myMethod) withObject:self afterDelay:1.0];
I have a data source with about 2000 lines that look like the following:
I have a huge log file where there are about 15 million lines. I
I have 2 simple questions about python: 1.How to get number of lines of
I have about 40 links which are all handled within jquery but when they
My situation is that I screwed up essentially. I inherited my code base about
I have a ASP.NET bulleted list control that, until today, was created and used

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.