frankfoerster / cakephp-asset
Provides a CakePHP 3.x AssetHelper to selectively add last modified timestamps to css and js assets.
Installs: 2 276
Dependents: 2
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Type:cakephp-plugin
Requires
- php: >=7.0.0
Requires (Dev)
- cakephp/cakephp: ~3.4
- cakephp/cakephp-codesniffer: ^3.0
- phpunit/phpunit: ^5.7.14|^6.0
This package is auto-updated.
Last update: 2024-11-07 10:39:42 UTC
README
-
Provides a CakePHP 3.x AssetHelper to selectively add last modified timestamps to css and js assets. CakePHP's implementation of Asset timestamps does not allow you to apply the behavior selectively to single files. Therefore I created this little Helper.
-
Provides a CakePHP 3.x AssetFilter to request asset files from
/src/Assets/*
. For example if you delevop an app using requirejs, then you can enable the AssetFilter to request modules from/src/Assets/js/*
. This enables you to hide your source files from the webroot of your application or plugin.
Installation
You can install this plugin into your CakePHP application using composer.
Run the following command
composer require frankfoerster/cakephp-asset
Configuration
You can load the plugin using the shell command:
bin/cake plugin load FrankFoerster/Asset
Or you can manually add the loading statement in the config/boostrap.php file of your application:
Plugin::load('FrankFoerster/Asset');
Load the Helper
Load the AssetHelper in the src/View/AppView.php file of your application.
... public function initialize() { $this->loadHelper('Asset', [ 'className' => 'FrankFoerster/Asset.Asset' ]); } ...
Use the AssetHelper in your Layout.
The last modified time of the asset files is automatically appended.
The methods provided are AssetHelper::css()
and AssetHelper::js()
. Both take up to three arguments:
$path
- The path to the asset relative to the webroot of your app or plugin.$plugin
- Either false to link to an app asset, or the name of a plugin. (default false)$appendTimestamp
- Whether to append a last modified timestamp to the url. (default true)
CSS
Linking the CSS file your_app/webroot/css/app.css
echo $this->Asset->css('css/app.css');
produces the following output:
<link rel="stylesheet" type="text/css" href="css/app.css?t=1460443221">
JS
Linking the JS file your_app/webroot/js/app.js
echo $this->Asset->js('js/app.js');
produces the following output:
<script type="text/javascript" src="js/app.js?t=1460443221"></script>
Linking Plugin Assets
Linking the JS file MyPlugin/webroot/js/plugin.js
echo $this->Asset->js('js/plugin.js', 'MyPlugin');
produces the following output:
<script type="text/javascript" src="my_plugin/js/plugin.js?t=1460443221"></script>
Linking Source Assets
To request assets from /src/Assets/*
you have to enable the AssetFilter in config/bootstrap.php
:
DispatcherFactory::add('FrankFoerster/Asset.Asset');
Then you can use the AssetHelper to request a source asset.
App
Linking the JS file your_app/src/Assets/js/app.js
echo $this->Asset->js('ASSETS/js/app.js');
produces the following output:
<script type="text/javascript" src="ASSETS/js/app.js?t=1460443221"></script>
and the AssetFilter will then return the content of your_app/src/Assets/js/app.js.
Plugin
Linking the JS file MyPlugin/src/Assets/js/plugin.js
echo $this->Asset->js('ASSETS/js/plugin.js', 'MyPlugin);
will procude the following output:
<script type="text/javascript" src="my_plugin/ASSETS/js/plugin.js?t=1460443221"></script>
and the AssetFilter will then return the content of MyPlugin/src/Assets/js/plugin.js.
Important!
Linking of source assets is only enabled for Configure 'debug' => true
.