CodeIgniter - accessing $config variable in view

CodeIgniter - accessing $config variable in view

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Accessing Configuration in CodeIgniter Views Without Controller Clutter

Developers frequently encounter the need to expose configuration values to their view layer. In CodeIgniter, this often manifests as a desire to access the $config array directly within a template file. While passing these variables explicitly via load->view() is functionally sound, it can feel verbose and repetitive when dealing with numerous settings like base URLs, site titles, or feature flags. The core question arises: can we retrieve this data in the view without forcing every controller to pass specific arguments?

The short answer is that CodeIgniter does not provide a built-in magic variable named $config within the view context. Unlike some frameworks that automatically inject an environment object or configuration instance, CodeIgniter treats views as passive rendering engines that expect data passed explicitly from the controller. Attempting to access a global $config array in a template will result in an undefined variable error because the superglobal $_SERVER, $_GET, or similar arrays do not contain application-specific settings loaded by the Config class.

To achieve the goal of reducing controller clutter, you must look at how the configuration system is initialized and where those values actually live. In standard CodeIgniter installations, configuration files are loaded into memory as part of the initialization process. However, they are not automatically exposed as a global variable accessible from views/.

If your primary objective is to avoid passing variables one by one in every controller, you should leverage the $this->load->vars() method or pass an associative array containing multiple keys at once. This approach keeps the code explicit and maintainable. For example:

$this->load->view('template', [
    'base_url' => config_item('base_url'),
    'site_title' => config_item('site_title')
]);

This pattern ensures that the values are fetched directly from the configuration array using config_item(), which is available in both controllers and views if loaded, but requires explicit passing to the view function.

Alternatively, you can utilize CodeIgniter's Hooks or a custom library to inject a global configuration object into your environment. By creating a library that returns an instance of the Config class or a specific array containing all loaded items, you can make this available globally. However, this introduces complexity and potential coupling issues, as views become dependent on a specific global state rather than receiving clean data injection.

A more robust architectural pattern involves using a dedicated configuration service or singleton pattern. You might create a helper function or a library called ConfigHelper that returns the necessary config array. This allows you to call ConfigHelper::get_all() in your controller and pass it once, significantly reducing repetitive code. While this requires an initial setup step, it centralizes the logic of configuration retrieval and prevents views from needing direct access to internal framework variables.

It is also worth noting that CodeIgniter 4 handles configuration slightly differently than previous versions, utilizing a service container and dependency injection. In CI 4, accessing config in views is typically done by passing an array of data to the view method or using a shared repository pattern via services. Trying to access global config variables directly without passing them breaks the encapsulation principles that modern frameworks aim to enforce.

Ultimately, the most maintainable solution respects the separation of concerns. Controllers handle data retrieval and preparation, while views focus on rendering. If you find yourself constantly needing to access raw $config in your templates, it might indicate a need to refactor how configuration is structured or consider migrating to a framework with more opinionated global state management, such as Laravel. For those sticking with CodeIgniter, explicit passing remains the standard best practice for ensuring clarity and avoiding subtle bugs caused by shared global state.

Our network