22Code Modules
33############
44
5- CodeIgniter supports a very simple form of modularization to help you create reusable code. Modules are typically
5+ CodeIgniter supports a form of code modularization to help you create reusable code. Modules are typically
66centered around a specific subject, and can be thought of as mini-applications within your larger application. Any
77of the standard file types within the framework are supported, like controllers, models, views, config files, helpers,
88language files, etc. Modules may contain as few, or as many, of these as you like.
@@ -17,7 +17,7 @@ The core element of the modules functionality comes from the :doc:`PSR4-compatib
1717that CodeIgniter uses. While any code can use the PSR4 autoloader and namespaces, the only way to take full advantage of
1818modules is to namespace your code and add it to **application/Config/Autoload.php **, in the ``psr4 `` section.
1919
20- For example, let's say we want to keep a simple blog module that we can re-use between components . We might create
20+ For example, let's say we want to keep a simple blog module that we can re-use between applications . We might create
2121folder with our company name, Acme, to store all of our modules within. We will put it right alongside our **application **
2222directory in the main project root::
2323
@@ -59,6 +59,41 @@ Of course, there is nothing forcing you to use this exact structure, and you sho
5959best suits your module, leaving out directories you don't need, creating new directories for Entities, Interfaces,
6060or Repositories, etc.
6161
62+ ==============
63+ Auto-Discovery
64+ ==============
65+
66+ Many times, you will need to specify the full namespace to files you want to include, but CodeIgniter can be
67+ configured to make integrating modules into your applications simpler by automatically discovering many different
68+ file types, including:
69+
70+ - :doc: `Events </general/events >`
71+ - :ref: `registrars `
72+ - :doc: `Route files </general/routing >`
73+ - :doc: `Services </concepts/services >`
74+
75+ This is configured in the file **application/Config/Modules.php **.
76+
77+ The auto-discovery system works by scanning any psr4 namespaces that have been defined within **Config/Autoload.php **
78+ for familiar directories/files.
79+
80+ When at the **acme ** namespace above, we would need to make one small adjustment to make it so the files could be found:
81+ each "module" within the namespace would have to have it's own namespace defined there. **Acme ** would be changed
82+ to **Acme\B log **. Once your module folder has been defined, the discover process would look for a Routes file, for example,
83+ at **/acme/Blog/Config/Routes.php **, just as if it was another application.
84+
85+ Enable/Disable Discover
86+ =======================
87+
88+ You can turn on or off all auto-discovery in the system with the **$enabled ** class variable. False will disable
89+ all discovery, optimizing performance, but negating the special capabilities of your modules.
90+
91+ Specify Discovery Items
92+ =======================
93+
94+ With the **$activeExplorers ** option, you can specify which items are automatically discovered. If the item is not
95+ present, then no auto-discovery will happen for that item, but the others in the array will still be discovered.
96+
6297==================
6398Working With Files
6499==================
@@ -70,24 +105,17 @@ guide, but is being reproduced here so that it's easier to grasp how all of the
70105Routes
71106======
72107
73- By default, :doc: `routes </general/routing >` are not automatically scanned for within modules. This is to boost
74- performance when modules are not in use. However, it's a simple thing to scan for any Routes file within modules.
75- Simply change the ``discoverLocal `` setting to true in **/application/Config/Routes.php **::
76-
77- $routes->discoverLocal(true);
78-
79- This will scan all PSR4 namespaced directories specified in **/application/Config/Autoload.php **. It will look for
80- **{namespace}/Config/Routes.php ** files and load them if they exist. This way, each module can contain its own
81- Routes file that is kept with it whenever you add it to new projects. For our blog example, it would look for
82- **/acme/Blog/Config/Routes.php **.
108+ By default, :doc: `routes </general/routing >` are automatically scanned for within modules. If can be turned off in
109+ the **Modules ** config file, described above.
83110
84111.. note :: Since the files are being included into the current scope, the ``$routes`` instance is already defined for you.
85112 It will cause errors if you attempt to redefine that class.
86113
87114Controllers
88115===========
89116
90- Controllers cannot be automatically routed by URI detection, but must be specified within the Routes file itself::
117+ Controllers outside of the main **application/Controllers ** directory cannot be automatically routed by URI detection,
118+ but must be specified within the Routes file itself::
91119
92120 // Routes.php
93121 $routes->get('blog', 'Acme\Blog\Controllers\Blog::index');
@@ -107,6 +135,8 @@ with the ``new`` command::
107135
108136 $config = new \Acme\Blog\Config\Blog();
109137
138+ Config files are automatically discovered whenever using the **config() ** function that is always available.
139+
110140Migrations
111141==========
112142
@@ -116,7 +146,7 @@ namespaces will be run every time.
116146Seeds
117147=====
118148
119- Seeds files can be used from both the CLI and called from within other seed files as long as the full namespace
149+ Seed files can be used from both the CLI and called from within other seed files as long as the full namespace
120150is provided. If calling on the CLI, you will need to provide double backslashes::
121151
122152 > php public/index.php migrations seed Acme\\Blog\\Database\\Seeds\\TestPostSeeder
0 commit comments