https://github.com/blacksmoke26/yii2cdn
A Yii Framework 2 component for using assets in different environments (Local/CDNs)
https://github.com/blacksmoke26/yii2cdn
assets cdn components management php yii2
Last synced: about 1 year ago
JSON representation
A Yii Framework 2 component for using assets in different environments (Local/CDNs)
- Host: GitHub
- URL: https://github.com/blacksmoke26/yii2cdn
- Owner: blacksmoke26
- License: apache-2.0
- Created: 2016-04-06T11:22:52.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2020-12-05T11:07:58.000Z (over 5 years ago)
- Last Synced: 2025-05-08T00:52:25.921Z (about 1 year ago)
- Topics: assets, cdn, components, management, php, yii2
- Language: PHP
- Size: 396 KB
- Stars: 14
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: history.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://packagist.org/packages/blacksmoke26/yii2cdn) [](https://packagist.org/packages/blacksmoke26/yii2cdn) [](https://packagist.org/packages/blacksmoke26/yii2cdn) [](https://packagist.org/packages/blacksmoke26/yii2cdn)
[](https://github.com/blacksmoke26/yii2cdn/issues)
[](https://github.com/blacksmoke26/yii2cdn/network)
[](https://github.com/blacksmoke26/yii2cdn/stargazers)
[](https://github.com/blacksmoke26/yii2cdn/wiki)
[](https://twitter.com/intent/tweet?text=Yii2cdn+extension:&url=https://github.com/blacksmoke26/yii2cdn)
# yii2cdn
A Yii Framework 2 component for using assets in different environments (Local/CDNs)
**Production Ready**: Used in several real projects is enough to prove its stability.
**Minimum requirements:** PHP 7.0+ / Yii2 Framework 2.0.12+
**Bugs / Feature Request?:** Create your [issue here](https://github.com/blacksmoke26/yii2cdn/issues).
## Resources
* **Wiki**: [https://github.com/blacksmoke26/yii2cdn/wiki](https://github.com/blacksmoke26/yii2cdn/wiki)
* **Class Reference**: [http://blacksmoke26.github.io/yii2cdn/api/](http://blacksmoke26.github.io/yii2cdn/api/)
## Installation
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```
composer require --prefer-dist blacksmoke26/yii2cdn "*"
```
or add
```
"blacksmoke26/yii2cdn": "*"
```
to the require section of your `composer.json` file.
## Usage
**Info:** *This tutorial will demonstrate how to use [`FancyBox3`](http://fancyapps.com/fancybox/3/) library in a production (online/CDN) or development (local/offline) environment.*
#### I. Installing a library
--------------------------
1. Create a cdn directory under the `/root/web` folder.
2. Install or download [`FancyBox3`](http://fancyapps.com/fancybox/3/) library under cdn directory.
* Path should be `/root/web/cdn/jquery-fancybox`.
* CDN URLs: https://cdnjs.com/libraries/fancybox/3.3.5
#### II. Add a component
---------------------
1. Open `@app/config/main.php` in your code editor.
2. Add a new property `cdn` under `components` section like the following code:
```php
// ...
'components' => [
// ...
'cdn' => [
'class' => '\yii2cdn\Cdn',
'baseUrl' => '/cdn',
'basePath' => dirname(__DIR__) . '/web/cdn',
'components' => [
'jquery-fancybox' => [
'css' => [
'@attributes' => [
'noNameInPathUrls' => true, // Hide /css in urls
],
[
'dist/jquery.fancybox.css', // offline version
'@cdn' => '//cdnjs.cloudflare.com/ajax/libs/fancybox/3.3.5/jquery.fancybox.min.css', // online version
],
],
'js' => [
'@attributes' => [
'noNameInPathUrls' => true, // Hide /js in urls
],
[
'dist/jquery.fancybox.js', // offline version
'@cdn' => '//cdnjs.cloudflare.com/ajax/libs/fancybox/3.3.5/jquery.fancybox.min.js', // online version
],
],
],
],
],
// ...
],
// ...
```
#### III. Registering assets
-------------------
1. Open any view file and paste the following line:
```php
//...
Yii::$app->cdn->get('jquery-fancybox')->register();
//...
```
#### IV. Final moment
1. Browse the action url in your browser and check the view souce.
> Now it's time to play around, See ya!