{"id":13609161,"url":"https://github.com/tattersoftware/codeigniter4-assets","last_synced_at":"2025-08-13T17:07:17.246Z","repository":{"id":34297706,"uuid":"175480165","full_name":"tattersoftware/codeigniter4-assets","owner":"tattersoftware","description":"Asset handling for CodeIgniter 4","archived":false,"fork":false,"pushed_at":"2024-01-18T15:31:09.000Z","size":249,"stargazers_count":49,"open_issues_count":3,"forks_count":12,"subscribers_count":6,"default_branch":"develop","last_synced_at":"2025-07-11T16:52:56.825Z","etag":null,"topics":["assets","codeigniter","codeigniter4","handling","loading"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tattersoftware.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-13T18:48:22.000Z","updated_at":"2025-02-06T03:12:19.000Z","dependencies_parsed_at":"2024-06-18T16:51:51.030Z","dependency_job_id":"f132c024-4613-4920-af07-80a4ae269aa5","html_url":"https://github.com/tattersoftware/codeigniter4-assets","commit_stats":{"total_commits":103,"total_committers":6,"mean_commits":"17.166666666666668","dds":"0.35922330097087374","last_synced_commit":"46db06058a283098248e8b7b4c8733a453f3e2a0"},"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"purl":"pkg:github/tattersoftware/codeigniter4-assets","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tattersoftware%2Fcodeigniter4-assets","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tattersoftware%2Fcodeigniter4-assets/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tattersoftware%2Fcodeigniter4-assets/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tattersoftware%2Fcodeigniter4-assets/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tattersoftware","download_url":"https://codeload.github.com/tattersoftware/codeigniter4-assets/tar.gz/refs/heads/develop","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tattersoftware%2Fcodeigniter4-assets/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270278345,"owners_count":24557172,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-13T02:00:09.904Z","response_time":66,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assets","codeigniter","codeigniter4","handling","loading"],"created_at":"2024-08-01T19:01:32.914Z","updated_at":"2025-08-13T17:07:17.196Z","avatar_url":"https://github.com/tattersoftware.png","language":"PHP","readme":"# Tatter\\Assets\n\nAsset handling for CodeIgniter 4\n\n[![](https://github.com/tattersoftware/codeigniter4-assets/workflows/PHPUnit/badge.svg)](https://github.com/tattersoftware/codeigniter4-assets/actions/workflows/test.yml)\n[![](https://github.com/tattersoftware/codeigniter4-assets/workflows/PHPStan/badge.svg)](https://github.com/tattersoftware/codeigniter4-assets/actions/workflows/analyze.yml)\n[![](https://github.com/tattersoftware/codeigniter4-assets/workflows/Deptrac/badge.svg)](https://github.com/tattersoftware/codeigniter4-assets/actions/workflows/inspect.yml)\n[![Coverage Status](https://coveralls.io/repos/github/tattersoftware/codeigniter4-assets/badge.svg?branch=develop)](https://coveralls.io/github/tattersoftware/codeigniter4-assets?branch=develop)\n\n## Quick Start\n\n1. Install with Composer: `\u003e composer require tatter/assets`\n2. Enable the `assets` filter in **app/Config/Filters.php**\n3. Assign `$routes` to their assets in **app/Config/Assets.php**\n\n## Features\n\nProvides automated asset loading for CSS and JavaScript files for CodeIgniter 4.\n\n## Installation\n\nInstall easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities\nand always be up-to-date:\n* `\u003e composer require tatter/assets`\n\nOr, install manually by downloading the source files and adding the directory to\n`app/Config/Autoload.php`.\n\n## Configuration\n\nThe library's default behavior can be overridden or augmented by its config file. Copy\n**examples/Assets.php** to **app/Config/Assets.php** and follow the instructions in the\ncomments. If no config file is found the library will use its default.\n\nIn order to use the `AssetsFilter` you must add apply it to your target routes. The filter\ndoes its own route matching so it is safe to apply it globally in **app/Config/Filters.php**.\nSee [Controller Filters](https://codeigniter.com/user_guide/incoming/filters.html) for more\ninfo, or the **Example** section below.\n\n## Usage\n\nIf installed correctly CodeIgniter 4 will detect and autoload the library, config, and filter.\n\n### Asset\n\nYou may use the `Asset` class to build a tag for a single asset file:\n```php\n\u003c?php\n\nuse Tatter\\Assets\\Asset;\n\n$asset = new Asset('\u003clink href=\"/assets/styles.css\" rel=\"stylesheet\" type=\"text/css\" /\u003e');\necho view('main', ['asset' =\u003e $asset]);\n```\n... then in your view file:\n\n```php\n\u003chtml\u003e\n\u003chead\u003e\n\t\u003ctitle\u003eHello World\u003c/title\u003e\n\t\u003c?= $asset ?\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n\t...\n```\n\nThe `Asset` class also comes with some named constructors to help you create the tag strings:\n* `createFromPath(string $path)` - Returns an `Asset` from a file relative to your config's `$directory`.\n* `createFromUri(string $uri, string $type = null)` - Returns an `Asset` from a remote URL, with an optional type (`css`, `js`, `img`; `null` to detect).\n\nNamed constructors make the above example much easier:\n```php\n\u003chtml\u003e\n\u003chead\u003e\n\t\u003ctitle\u003eHello World\u003c/title\u003e\n\t\u003c?= \\Tatter\\Assets\\Asset::createFromPath('styles.css') ?\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n\t...\n```\n\n### Bundle\n\nTypically a project will need more than one single asset. The `Bundle` class allows you to collect\nmultiple `Asset`s into a single instance. Use the `head()` and `body()` methods to return the `Asset`s\ndestined for each tag, formatted as blocks of tags.\n\n`Bundle`s can be created one of two ways.\n\n#### Class Properties\n\nCreate your own `Bundle` class and use these properties to stage the assets you want it to have:\n\t* `$bundles`: Names of other `Bundle` classes to merge with.\n\t* `$paths`: Relative file paths to make into `Asset`s.\n\t* `$uris`: URLs to make into `Asset`s.\n\t* `$strings`: Direct strings to pass into an `Asset`.\n\nExample:\n```php\n\u003c?php namespace App\\Bundles;\n\nuse Tatter\\Assets\\Bundle;\n\nclass FrontendBundle extends Bundle\n{\n    protected $bundles = [\n        StylesBundle::class,\n    ];\n\n    protected $paths = [\n        'bootstrap/dist/css/bootstrap.min.css',\n        'bootstrap/dist/js/bootstrap.bundle.min.js',\n    ];\n\n    protected $uris = [\n        'https://pagecdn.io/lib/cleave/1.6.0/cleave.min.js',\n    ];\n}\n```\n\n#### define()\n\n`Bundle` also comes with an initialization method: `define()`. Supply your own version of this\nmethod along with the fluent-style definition methods to create more complicated collections.\n\nExample:\n```php\n\u003c?php namespace App\\Bundles;\n\nuse Tatter\\Assets\\Asset;\nuse Tatter\\Assets\\Bundle;\n\nclass ColorBundle extends Bundle\n{\n    protected function define()\n    {\n        $this\n            -\u003eadd(Asset::createFromPath('styles.css')) // Add individual Assets\n            -\u003emerge($someOtherBundle); // Or combine multiple Bundles\n\n        // Create more complex Assets\n        $source = '\u003cscript src=\"https://pagecdn.io/lib/cleave/1.6.0/cleave.min.js\" type=\"text/javascript\"\u003e\u003c/script\u003e';\n        $inHead = true; // Force a JavaScript Asset to the \u003chead\u003e tag\n        $asset  = new Asset($source, $inHead);\n    }\n}\n```\n\n### Filter\n\nIf you configured the `AssetsFilter` (see above) to load for your routes, you must also associate\nthe specific assets or bundles per route. Use the config ``$routes`` property, where the route\npattern is the key and the values are arrays of file paths, URLs, or bundle class names. E.g.:\n\n```php\n\u003c?php namespace Config;\n\nuse Tatter\\Assets\\Config\\Assets as AssetsConfig;\n\nclass Assets extends AssetsConfig\n{\n    public $routes = [\n        '*' =\u003e [\n            'bootstrap/bootstrap.min.css',\n            'bootstrap/bootstrap.bundle.min.js',\n            'font-awesome/css/all.min.css',\n            'styles/main.css',\n        ],\n        'files' =\u003e [\n            'dropzone/dropzone.min.css',\n            'dropzone/dropzone.min.js',\n        ],\n    ];\n}\n```\n\nIf you apply the filter via your Routes config file you may also supply bundle class names\nas arguments to merge them with any other configured route bundles:\n```php\n// **app/Config/Routes.php**\n$routes-\u003eadd('files', 'Files::index', ['filter' =\u003e 'assets:\\App\\Filters\\FilesFilter']);\n```\n\n## Example\n\nYou want to make a simple web app for browsing and uploading files, based on Bootstrap's\nfrontend. Start your CodeIgniter 4 project, then add Bootstrap and DropzoneJS to handle\nthe uploads:\n\n\tcomposer require twbs/bootstrap enyo/dropzone\n\n\u003e Note: You will need to copy files from **vendor** to **public/assets/** to make them\n\taccessible, or use the framework's `Publisher` class to handle this for you.\n\nAdd this module as well:\n\n\tcomposer require tatter/assets\n\nEdit your **Filters.php** config file to enable the `AssetsFilter` on all routes:\n\n```php\n    /**\n     * List of filter aliases that are always\n     * applied before and after every request.\n     *\n     * @var array\n     */\n    public $globals = [\n        'before' =\u003e [\n            // 'honeypot',\n            // 'csrf',\n        ],\n        'after'  =\u003e [\n            'assets' =\u003e ['except' =\u003e 'api/*'],\n        ],\n    ];\n```\n\nCreate a new `Bundle` to define your Bootstrap files in **app/Bundles/DropzoneJS.php**:\n\n```php\n\u003c?php namespace App\\Bundles;\n\nuse Tatter\\Assets\\Bundle;\n\nclass DropzoneJS extends Bundle\n{\n    protected $paths = [\n        'dropzone/dropzone.min.css',\n        'dropzone/dropzone.min.js',\n    ];\n}\n```\n\nThen copy **examples/Assets.php** from this repo to **app/Config/** and edit it so Bootstrap\nwill load on every route and DropzoneJS will load on specific routes:\n\n```php\npublic $routes = [\n    '*' =\u003e [\n        'bootstrap/dist/css/bootstrap.min.css',\n        'bootstrap/dist/js/bootstrap.bundle.min.js',\n    ],\n    'files/*' =\u003e [\n        \\App\\Bundles\\DropzoneJS::class,\n    ],\n    'upload' =\u003e [\n        \\App\\Bundles\\DropzoneJS::class,\n    ],\n];\n```\n\n\u003e Note: We could have made a `Bundle` for Bootstrap as well but since they are only needed for one route this is just as easy.\n\nIf you finished all that then your assets should be injected into your `\u003chead\u003e` and `\u003cbody\u003e` tags accordingly.\n\nYour view file:\n```html\n\u003chtml\u003e\n\u003chead\u003e\n\t\u003ctitle\u003eFile Upload\u003c/title\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n\t\u003ch1\u003eHello\u003c/h1\u003e\n\t\u003cp\u003ePut your upload form here.\u003c/p\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\n... served as:\n```html\n\u003chtml\u003e\n\u003chead\u003e\n\t\u003ctitle\u003eFile Upload\u003c/title\u003e\n\n\u003clink href=\"http://example.com/assets/bootstrap/dist/css/bootstrap.min.css?v=1234151511412\" rel=\"stylesheet\" type=\"text/css\" /\u003e\n\u003clink href=\"http://example.com/assets/dropzone/dropzone.min.css?v=12341515141241\" rel=\"stylesheet\" type=\"text/css\" /\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n\t\u003ch1\u003eHello\u003c/h1\u003e\n\t\u003cp\u003ePut your upload form here.\u003c/p\u003e\n\n\u003cscript src=\"http://example.com/assets/bootstrap/dist/js/bootstrap.bundle.min.js?v=12341515735743\" type=\"text/javascript\"\u003e\u003c/script\u003e\n\u003cscript src=\"http://example.com/assets/dropzone/dropzone.min.js?v=12341515573424\" type=\"text/javascript\"\u003e\u003c/script\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\n## Vendor Classes\n\nThis library includes two abstract class stubs to ease working with third-party assets.\n`VendorPublisher` is a wrapper for the framework's [Publisher Library](https://codeigniter.com/user_guide/libraries/publisher.html)\nprimed for use with `Assets`, and `VendorBundle` is a specialized version of this library's\n`Bundle` primed to handle assets published via `VendorPublisher`. Together these two classes\ncan take a lot of the work out of managing assets you include from external sources.\n\nLet's revisit the example above... Instead of copies the files into **public/assets/** ourselves\n(and re-copying every time there is an update) let's create a `VendorPublisher` to do that\nfor us. In **app/Publishers/BootstrapPublisher.php**:\n```php\n\u003c?php\n\nnamespace App\\Publishers;\n\nuse Tatter\\Assets\\VendorPublisher;\n\nclass BootstrapPublisher extends VendorPublisher\n{\n    protected $source = 'vendor/twbs/bootstrap/dist';\n    protected $path   = 'bootstrap';\n}\n\n```\n\nThat's all! `VendorPublisher` knows that `$path` is relative the to directory in your Assets\nconfig file, so when you run `php spark publish` next all the latest Bootstrap assets will\nbe copied into that directory (default: **public/assets/vendor/**).\n\n\u003e Note: Since these are external dependencies be sure to exclude them from your repo with your **.gitignore** file.\n\nNow lets use these assets. We can create a new `VendorBundle` and use the new `addPath()`\nmethod to access the same files we just published from Composer's vendor directory.\nIn **app/Bundles/BootstrapBundle.php**:\n```php\n\u003c?php\n\nnamespace App\\Bundles;\n\nuse Tatter\\Assets\\VendorBundle;\n\nclass BootstrapBundle extends VendorBundle\n{\n    protected function define(): void\n    {\n        $this\n            -\u003eaddPath('bootstrap/bootstrap.min.css')\n            -\u003eaddPath('bootstrap/bootstrap.bundle.min.js');\n    }\n}\n```\n\nNow add the new bundle to our **app/Config/Assets.php** routes:\n```php\npublic $routes = [\n    '*' =\u003e [\\App\\Bundles\\BootstrapBundle::class],\n];\n```\n\nAnd we have hands-free Bootstrap updates from now on!\n\n## Testing\n\nThis library includes some PHPUnit extension classes in **src/Test/** to assist with testing\nAssets and Bundles. These are used to test the files from this library but are also available\nfor your own libraries and projects to use. Simply extend the appropriate test case and add\na data provider method with your class name and criteria to meet. See the test files in\n**tests/** for examples.\n","funding_links":[],"categories":["PHP"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftattersoftware%2Fcodeigniter4-assets","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftattersoftware%2Fcodeigniter4-assets","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftattersoftware%2Fcodeigniter4-assets/lists"}