Code Modules

CodeIgniter supports a form of code modularization to help you create reusable code. Modules are typicallycentered around a specific subject, and can be thought of as mini-applications within your larger application. Anyof the standard file types within the framework are supported, like controllers, models, views, config files, helpers,language files, etc. Modules may contain as few, or as many, of these as you like.

Namespaces

The core element of the modules functionality comes from the PSR4-compatible autoloadingthat CodeIgniter uses. While any code can use the PSR4 autoloader and namespaces, the primary way to take full advantage ofmodules is to namespace your code and add it to app/Config/Autoload.php, in the psr4 section.

For example, let’s say we want to keep a simple blog module that we can re-use between applications. We might createfolder with our company name, Acme, to store all of our modules within. We will put it right alongside our applicationdirectory in the main project root:

  1. /acme // New modules directory
  2. /application
  3. /public
  4. /system
  5. /tests
  6. /writable

Open app/Config/Autoload.php and add the Acme namespace to the psr4 array property:

  1. $psr4 = [
  2. 'Config' => APPPATH . 'Config',
  3. APP_NAMESPACE => APPPATH, // For custom namespace
  4. 'App' => APPPATH, // To ensure filters, etc still found,
  5. 'Acme' => ROOTPATH.'acme'
  6. ];

Now that this is set up, we can access any file within the acme folder through the Acme namespace. This alonetakes care of 80% of what is needed for modules to work, so you should be sure to familiarize yourself with namespacesand become comfortable with their use. Several file types will be scanned for automatically through all defined namespaces - a crucial ingredient for working with modules.

A common directory structure within a module will mimic the main application folder:

  1. /acme
  2. /Blog
  3. /Config
  4. /Controllers
  5. /Database
  6. /Migrations
  7. /Seeds
  8. /Helpers
  9. /Language
  10. /en
  11. /Libraries
  12. /Models
  13. /Views

Of course, there is nothing forcing you to use this exact structure, and you should organize it in the manner thatbest suits your module, leaving out directories you don’t need, creating new directories for Entities, Interfaces,or Repositories, etc.

Auto-Discovery

Many times, you will need to specify the full namespace to files you want to include, but CodeIgniter can beconfigured to make integrating modules into your applications simpler by automatically discovering many differentfile types, including:

This is configured in the file app/Config/Modules.php.

The auto-discovery system works by scanning any psr4 namespaces that have been defined within Config/Autoload.phpfor familiar directories/files.

When at the acme namespace above, we would need to make one small adjustment to make it so the files could be found:each “module” within the namespace would have to have it’s own namespace defined there. Acme would be changedto AcmeBlog. Once your module folder has been defined, the discover process would look for a Routes file, for example,at /acme/Blog/Config/Routes.php, just as if it was another application.

Enable/Disable Discover

You can turn on or off all auto-discovery in the system with the $enabled class variable. False will disableall discovery, optimizing performance, but negating the special capabilities of your modules.

Specify Discovery Items

With the $activeExplorers option, you can specify which items are automatically discovered. If the item is notpresent, then no auto-discovery will happen for that item, but the others in the array will still be discovered.

Discovery and Composer

Packages that were installed via Composer will also be discovered by default. This only requires that the namespacethat Composer knows about is a PSR4 namespace. PSR0 namespaces will not be detected.

If you do not want all of Composer’s known directories to be scanned when locating files, you can turn this offby editing the $discoverInComposer variable in Config\Modules.php:

  1. public $discoverInComposer = false;

Working With Files

This section will take a look at each of the file types (controllers, views, language files, etc) and how they canbe used within the module. Some of this information is described in more detail in the relevant location of the userguide, but is being reproduced here so that it’s easier to grasp how all of the pieces fit together.

Routes

By default, routes are automatically scanned for within modules. It can be turned off inthe Modules config file, described above.

Note

Since the files are being included into the current scope, the $routes instance is already defined for you.It will cause errors if you attempt to redefine that class.

Controllers

Controllers outside of the main app/Controllers directory cannot be automatically routed by URI detection,but must be specified within the Routes file itself:

  1. // Routes.php
  2. $routes->get('blog', 'Acme\Blog\Controllers\Blog::index');

To reduce the amount of typing needed here, the group routing feature is helpful:

  1. $routes->group('blog', ['namespace' => 'Acme\Blog\Controllers'], function($routes)
  2. {
  3. $routes->get('/', 'Blog::index');
  4. });

Config Files

No special change is needed when working with configuration files. These are still namespaced classes and loadedwith the new command:

  1. $config = new \Acme\Blog\Config\Blog();

Config files are automatically discovered whenever using the config() function that is always available.

Migrations

Migration files will be automatically discovered within defined namespaces. All migrations found across allnamespaces will be run every time.

Seeds

Seed files can be used from both the CLI and called from within other seed files as long as the full namespaceis provided. If calling on the CLI, you will need to provide double backslashes:

  1. > php public/index.php migrations seed Acme\\Blog\\Database\\Seeds\\TestPostSeeder

Helpers

Helpers will be located automatically from defined namespaces when using the helper() method, as long as itis within the namespaces Helpers directory:

  1. helper('blog');

Language Files

Language files are located automatically from defined namespaces when using the lang() method, as long as thefile follows the same directory structures as the main application directory.

Libraries

Libraries are always instantiated by their fully-qualified class name, so no special access is provided:

  1. $lib = new \Acme\Blog\Libraries\BlogLib();

Models

Models are always instantiated by their fully-qualified class name, so no special access is provided:

  1. $model = new \Acme\Blog\Models\PostModel();

Views

Views can be loaded using the class namespace as described in the views documentation:

  1. echo view('Acme\Blog\Views\index');