|
1 | 1 | # codeigniter4-assets |
2 | 2 | Asset publishing and loading for CodeIgniter 4 |
| 3 | + |
| 4 | +## Quick Start |
| 5 | + |
| 6 | +1. Install with Composer: `> composer require codenom/assets` |
| 7 | +2. Put CSS & JS files in: **public/assets** |
| 8 | +3. Add additional assets to config: **app/Config/Assets.php** |
| 9 | +3. Add in head tag: `<?= service('assets')->css() ?>` |
| 10 | +4. Add to footer: `<?= service('assets')->js() ?>` |
| 11 | + |
| 12 | +## Features |
| 13 | + |
| 14 | +Provides out-of-the-box asset loading for CSS and JavaScript files for CodeIgniter 4 |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +Install easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities |
| 19 | +and always be up-to-date: |
| 20 | +* `> composer require codenom/assets` |
| 21 | + |
| 22 | +Or, install manually by downloading the source files and adding the directory to |
| 23 | +`app/Config/Autoload.php`. |
| 24 | + |
| 25 | +## Configuration (optional) |
| 26 | + |
| 27 | +The library's default behavior can be overridden or augment by its config file. Copy |
| 28 | +**examples/Assets.php** to **app/Config/Assets.php** and follow the instructions in the |
| 29 | +comments. If no config file is found the library will use its defaults. |
| 30 | + |
| 31 | +## Usage |
| 32 | + |
| 33 | +If installed correctly CodeIgniter 4 will detect and autoload the library, service, and |
| 34 | +config. Use the library methods `css()` and `js()` to display tags for the route-specific assets: |
| 35 | +`<?= service('assets')->css() ?>` |
| 36 | + |
| 37 | +## Structure |
| 38 | + |
| 39 | +The library searches the assets directory (default: **public/assets**) for files matching |
| 40 | +the current route, loading them in a cascading fashion for each route segment. |
| 41 | + |
| 42 | +**Example:** https://example.com/users/view/30 |
| 43 | + |
| 44 | +The library will first load any root assets (`public/assets/*.css *.js`), then assets in |
| 45 | +the "users" subfolder (`public/assets/users/`), then "view" subfolder, then "12" subfolder. |
| 46 | +Any missing or invalid folders are ignored. |
| 47 | + |
| 48 | +Additional assets may be specified from the config variable `$routes` - this is particularly |
| 49 | +helpful for including pre-bundled libraries. `$routes` maps each route to an asset file or |
| 50 | +a directory of assets to load for that route. |
| 51 | + |
| 52 | +**Example:** |
| 53 | + |
| 54 | +``` |
| 55 | +public $routes = [ |
| 56 | + '' => [ |
| 57 | + 'bootstrap/dist/css/bootstrap.min.css', |
| 58 | + 'bootstrap/dist/js/bootstrap.bundle.min.js' |
| 59 | + ], |
| 60 | + 'files/upload' => [ |
| 61 | + 'vendor/dropzone' |
| 62 | + ] |
| 63 | +]; |
| 64 | +``` |
| 65 | + |
| 66 | +This tells the library to load the Bootstrap assets for every route (`''`) without having |
| 67 | +to move it from its pre-bundled subdirectory. It also will load any assets in the `dropzone` |
| 68 | +directory for the specified route. |
0 commit comments