https://github.com/white43/yii2-cloud-asset-manager
This extension uploads your local yii2 assets to CDN services
https://github.com/white43/yii2-cloud-asset-manager
assets assets-management cloud google s3 yii2 yii2-extension
Last synced: 8 months ago
JSON representation
This extension uploads your local yii2 assets to CDN services
- Host: GitHub
- URL: https://github.com/white43/yii2-cloud-asset-manager
- Owner: white43
- License: mit
- Created: 2020-05-11T18:36:12.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-08-12T09:37:16.000Z (about 2 years ago)
- Last Synced: 2025-01-31T09:33:59.608Z (8 months ago)
- Topics: assets, assets-management, cloud, google, s3, yii2, yii2-extension
- Language: PHP
- Homepage:
- Size: 34.2 KB
- Stars: 6
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
#### What it is
This small library is useful when you need to have repeatable hashes of your
asset bundles (thus, repeatable URLs of `css`, `js`, etc.) across deploys. It
helps to keep CDN cache warm and declines the number of the requests to the
origin. Nevertheless, it may be used in production environment as is without
uploading asset bundles to any remote storage. It relies on the contents of
your assets not on their timestamps, so it generates constant hashes and
caches them.#### Installation
The preferred way to install this extension is to use `composer`.
```Shell
composer require white43/yii2-cloud-asset-manager
```Next, you need to choose and install an appropriate adapter (which knows how
to talk to different storages) from the list of the supported adapters. They
can be found [here](https://github.com/thephpleague/flysystem#officially-supported-adapters).For instance:
```Shell
composer require league/flysystem-aws-s3-v3
```#### Cloud configuration
```PHP
[
'assetManager' => [
'class' => \white43\CloudAssetManager\CloudAssetManager::class,
'basePath' => 'local/path/to/assets',
'baseUrl' => '//your.cdn.com/remote/path/to/assets',
'cache' => 'cache', // Name of your cache component
'verbose' => true, // To dump copying process to stdout
'adapter' => function (): \League\Flysystem\FilesystemAdapter {
$s3 = new \Aws\S3\S3Client([
'credentials' => [
'key' => 'Access Key ID',
'secret' => 'Secret Access Key',
],
'region' => 'eu-central-1',
'version' => 'latest',
// The following options are useful when you need to connect to a S3-compatible storage
// 'endpoint' => '',
// 'use_path_style_endpoint' => true,
]);return new \League\Flysystem\AwsS3V3\AwsS3V3Adapter($s3, 'Your bucket name');
},
],
],
];
```Your asset bundles will be automatically uploaded to the chosen cloud storage.
On the browser side users will get files from that storage. Cache component
will be used to speed up page load.If you have already configured filesystem for any other purposes as a component,
it is possible pass the name of the filesystem component.```PHP
[
'fs' => [
'class' => \League\Flysystem\Filesystem::class,
// ...
],
'assetManager' => [
'class' => \white43\CloudAssetManager\CloudAssetManager::class,
// ...
'filesystem' => 'fs',
],
],
];
```If you prefer having components in Dependency Injection Container, it is not a
problem.```PHP
set(\League\Flysystem\Filesystem::class, function () {
// ...
return new \League\Flysystem\Filesystem(...);
})$config = [
'components' => [
'assetManager' => [
'class' => \white43\CloudAssetManager\CloudAssetManager::class,
// ...
'filesystem' => \League\Flysystem\Filesystem::class,
],
],
];
```#### Local configuration
```PHP
[
'assetManager' => [
'class' => \white43\CloudAssetManager\LocalAssetManager::class,
'cache' => 'cache', // Name of your cache component
'basePath' => '@app/web/assets', // @webroot doesn't exist in CLI mode
],
],
];
```Assets will not be uploaded to any cloud storage. This way may be useful for
testing and/or when you just need to keep constant hashes for your assets
without any cloud storage.#### Upload your assets in the background
Add some configuration to your `console.php`.
```PHP
[
'warm-up' => \white43\CloudAssetManager\commands\WarmUpController::class,
],
];
```Add some configuration to your `params.php`.
```PHP
[
\app\assets\AppAsset::class,
],
];
```Run next command in the background (i.e. when a container is starting)
```Shell
./yii warm-up
```