Skip to content

CDN Class

Junaid Atari edited this page Aug 25, 2016 · 4 revisions

Cdn API docs

How to access ?

/** @var \yii2cdn\Cdn $cdn */
$cdn = \Yii::$app->cdn;

Useful Methods:

get(string $id, [bool $throwException = true])

Type: \yii2cdn\Component

Get a component by ID.

/** @var \yii2cdn\Component $component */
$component = $cdn->get('font-awesome');

exists(string $id, [bool $throwException = true])

Type: boolean

Check that component exists.

// false to disable throwing exception
var_dump( $cdn->exists('font-awesome', false) );
// Output: bool(true)

getUrl([string|array $str = null])

Type: string | array

Get the url of CDN.

1. Url only.

echo $cdn->getUrl();
// Output: /cdn

2. Append url with filename.

echo $cdn->getUrl('js/bundle.min.js');
// Output: /cdn/js/bundle.min.js

3. Append url with multiple filename.

\yii\helpers\VarDumper::dump( $cdn->getUrl([
	'bundles/all.min.js',
    'bundles/all.min.css',
]), 10, true );
// Output:
/*
[
  0 => '/cdn/bundle/all.min.js'
  1 => '/cdn/bundle/all.min.css'
]
*/

getPath([string|array $str = null])

Type: string | array

Get the base path of CDN direcory.

1. Path only.

echo $cdn->getPath();
// Output: /var/www/html/cdn

2. Append path with filename.

echo $cdn->getPath('js/bundle.min.js');
// Output: /var/www/html/cdn/js/bundle.min.js

3. Append path with multiple filename.

\yii\helpers\VarDumper::dump( $cdn->getPath([
	'bundles/all.min.js',
    'bundles/all.min.css',
]), 10, true );
// Output:
/*
[
  0 => '/var/www/html/cdn/bundle/all.min.js'
  1 => '/var/www/html/cdn/bundle/all.min.css'
]
*/

getSectionByRoot(string $root, [bool $throwException = true])

Type: \yii2cdn\Section

Get a component section by root.

//root = component-id/section-id
/** @var \yii2cdn\Section $section */
$section = $cdn->getFileByRoot('font-awesome/css');

getFileByRoot(string $root, [bool $asUrl = false], [bool $throwException = true])

Type: \yii2cdn\File | string

Get a component section's file by root.

1. File object.

//root = component-id/section-id/file-id
/** @var \yii2cdn\File $file */
$file = $cdn->getFileByRoot('font-awesome/css/core');

2. File url only.

echo $cdn->getFileByRoot('font-awesome/css/core', true);
// Output: ../cdn/font-awesome/css/font-awesome.min.css

whenOnline(callable $callback)

Type: void

Execute a callback function when CDN status is online.

/** Example 1: (online) */
if ( \yii2cdn\Cdn::isOnline() ) {
	// some logic goes here
}

/** Example 2: (online event) */
\Yii::$app->cdn->whenOnline (function ($cdn) {
	/** @var \yii2cdn\Cdn $cdn */
    // some logic goes here
});

whenOffline(callable $callback)

Type: void

Execute a callback function when CDN status is offline.

/** Example 1: (offline) */
if ( !\yii2cdn\Cdn::isOnline() ) {
	// some logic goes here
}

/** Example 2: (offline event) */
\Yii::$app->cdn->whenOffline (function ($cdn) {
	/** @var \yii2cdn\Cdn $cdn */
    // some logic goes here
});

refresh()

Type: void

Clears the cdn cache and repopulate components.

$cdn->refresh();

Note: the red links below are pages yet to be created. Feel free to add them!

Overview documents

  • README: distributed with yii2cdn, contains a quick overview of yii2cdn's functionality, an example, and the license.
  • REFERENCE: Containing incomplete api class reference.

Quick Start

Configuration

Tutorials

Clone this wiki locally