1

I want to add some code that will run for every controller. Adding the code to CodeIgniter's CI_CONTROLLER class seems unconventional.

Where is the right place to include code you want to run for every controller?

Here is the code:

require_once 'vendor/autoload.php';

$bugsnag = Bugsnag\Client::make("my-secret-key-is-here");
Bugsnag\Handler::register($bugsnag);

These classes both come from a dependency installed with Composer.

I suspect I should create a helper, and include it in application/config/autoload.php. But new to CodeIgniter, so not sure of conventions.

Edit: I am using CodeIgniter 3.1.6.

Don P
  • 60,113
  • 114
  • 300
  • 432

4 Answers4

3

If you want to execute arbitrary code at different points in CodeIgniter's life cycle, you can use the hooks feature.

Official Documentation: https://codeigniter.com/user_guide/general/hooks.html

1. Enable hooks

  • Go to /application/config/config.php.
  • Search for enable_hooks and set it to true: $config['enable_hooks'] = TRUE;

2. Add desired code to CodeIgniter's hook file:

  • Go to /application/config/hooks.php.
  • Choose the desired lifecycle to hook into (see doc link above for a list)

  • Add code to the lifecycle, e.g. $hook['pre_controller'] = function(){... your code goes here ...}

For this question's example, my hooks.php looks like this:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

// This is the code I added:
$hook['pre_system'] = function(){
  require_once 'vendor/autoload.php';

  $bugsnag = Bugsnag\Client::make("my-client-key");
  Bugsnag\Handler::register($bugsnag);
}

?>
Don P
  • 60,113
  • 114
  • 300
  • 432
2

I would just extend the Controller Class.

See "Extending Core Class":

"If all you need to do is add some functionality to an existing library - perhaps add a method or two - then it’s overkill to replace the entire library with your version. In this case it’s better to simply extend the class."

...

"Tip: Any functions in your class that are named identically to the methods in the parent class will be used instead of the native ones (this is known as “method overriding”). This allows you to substantially alter the CodeIgniter core."

class MY_Controller extends CI_Controller {

    ....

}

Any function you put inside would be added to the core, otherwise, if you use the same name as an existing method, it would replace just that one method.

You'd name it MY_Controller.php and put it inside application/core/, where it's picked up to override CI_Controller automatically.

If you are extending the Controller core class, then be sure to extend your new class in your application controller’s constructors.

class Welcome extends MY_Controller {

        public function __construct()
        {
                parent::__construct();
                // Your own constructor code
        }

        public function index()
        {
                $this->load->view('welcome_message');
        }
}

Looks like you could also use a pre_system or pre_controller hook as described here:

https://www.codeigniter.com/user_guide/general/hooks.html

Community
  • 1
  • 1
Sparky
  • 98,165
  • 25
  • 199
  • 285
  • Seems like this would require every controller to extend MY_Controller instead of CI_Controller, and if you have an existing application update every controller. Is this correct? There's really no other place to just globally include 2 lines of code? – Don P Nov 10 '17 at 19:28
  • Got it - will give it a try now – Don P Nov 10 '17 at 19:33
  • Still seems messy - this is also a system wide concern, not just a controller. It looks like CodeIgniter has 'hooks'. What do you think about using /application/config/hooks.php, and creating a `$hook['pre_system']`? – Don P Nov 10 '17 at 19:52
  • @DonP, I've never used them, but it appears you can run a hook before each controller. – Sparky Nov 10 '17 at 19:56
  • Thanks for the help Sparky! I upvoted your answer since it's valid. Also posted how to do it using hooks as an alternate solution. – Don P Nov 10 '17 at 20:02
  • Hooks are not, IMO, any less messy. Because a controller is ALWAYS instantiated a custom class extending `CI_Controller` is the perfect place to include your code. It's a trivial task to refactor your controllers to extend `MY_Controller`. – DFriend Nov 10 '17 at 20:22
0

Go to application/config/config.php and set $config['composer_autoload'] = FALSE; to TRUE:

Enabling this setting will tell CodeIgniter to look for a Composer package auto-loader script in application/vendor/autoload.php.

As a result you need not to call require_once 'vendor/autoload.php';.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
Shamsur Rahman
  • 283
  • 5
  • 19
  • That is not answering the question. You need to include your own code as well, not just load things from composer. Also you should explain the reason behind code in your answer so people know why :) – Don P Nov 10 '17 at 20:22
  • Thanks Kanon, but you're still not answering the question, see my comment above. Specifically this answer never runs `$bugsnag = Bugsnag\Client::make("my-secret-key-is-here"); Bugsnag\Handler::register($bugsnag);`. – Don P Nov 10 '17 at 21:13
0

Assuming it's CodeIgnitor 3.X Go to application/config/config.php and change:

$config['composer_autoload'] = FALSE;

to

$config['composer_autoload'] = TRUE;

just below the above line include

require_once APPPATH.'vendor/autoload.php';

Or in your controller include

require_once APPPATH.'vendor/autoload.php';

From How to use composer packages in codeigniter?

Eje
  • 354
  • 4
  • 8
joash
  • 2,205
  • 2
  • 26
  • 31
  • | Enabling this setting will tell CodeIgniter to look for a Composer | package auto-loader script in application/vendor/autoload.php. | | $config['composer_autoload'] = TRUE; | | Or if you have your vendor/ directory located somewhere else, you | can opt to set a specific path as well: | | $config['composer_autoload'] = '/path/to/vendor/autoload.php'; | | For more information about Composer, please visit http://getcomposer.org/ | | Note: This will NOT disable or override the CodeIgniter-specific | autoloading (application/config/autoload.php) – Code GuruDev Jun 10 '22 at 05:25