Skip to content
Junaid Atari edited this page Aug 23, 2016 · 5 revisions

How to define files

Note: Before you continue, I suggest you to read the Component and Section wiki pages.

Simple

// Syntax
'component-id' => [
    // ...
    'section1' => [
    	/* files goes here
          --------------------
        */        
        # 1. As a string with or without / at beginning
        'somefile.ext',
        # 2. As an array (very first element without a key)
        ['dir/somefile2.ext']
        # 3. As an array with properties (First element should be a filename and without a key)
        ['dir/somefile3.ext', '@property'=>'mixed-value']
    	//... 
    ],
    // other sections and their files
]

@ Optional Properties

@id

string

Unique ID of the file. A valid name contains [a-z][a-zA-Z-0-9]+. By default each file will be generated with an automatic uinque id. This property will override the automatic generated file id.

Automatic generated id starts with * char.

// Example
'section' => [
    // ...
    ['icons/font-awesome-min.css', '@id'=>'main']
    // ...
]

@cdn

string

That file url used to be in a production environment. When the YII2CD_OFFLINE set to false. That @cdn file path will be used in the place of main file path. (see Force using offline files )

Value starts with http/https or // will use as it is.

// Example
'section' => [
    // ...
    // Example 1: (leave it to the protocol[http or https])
    ['icons/font-awesome-min.css', '@cdn'=>'//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css']
    // Example 2: (http only)
    ['icons/font-awesome.css', '@cdn'=>'http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css']
    // Example 3: (https only)
    ['icons/font-awesome.css', '@cdn'=>'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css']
    // Example 4: (minified version)
    ['icons/font-awesome.css', '@cdn'=>'/icons/font-awesome-min.css']
    // ...
]

@offline

boolean

Use this file as offline only. When the YII2CD_OFFLINE set to false, it will be skipped. (see Force using offline files )

// Example
'section' => [
    // ...
    ['icons/font-awesome-min.css', '@offline'=>true]
    // ...
]

@options

array

A Key=>Value pair of array as $options.

(see the $options parameter of registerJsFile($url, $options = []) and registerCssFile($url, $options = []) method in a \yii\web\View class.

// Example
'section' => [
    // ...
    ['icons/font-awesome-min.css', '@options'=> [
    	// The HTML attributes for the link tag goes here ... 
    ]]
    // ...
]

Additional attributes

mixed

A key=>value pair of an array where key starts without @ char and value can be a mixed value.

// Example

'font-awesome' => [
    // ...
    'css' => [
    	// ...
    	['icons/font-awesome-min.css', '@id'=>'libmain', 'attribute'=>'value']
    	// ...
    ],
    // ...
],
How to access attribute?
/** @var \yii2cdn\File $file */
$file = \Yii::$app->cdn->getFileByRoot('font-awesome/css/libmain');

// Get attribute value
echo $file->getAttr('attribute');
// output: value

// Update/Set attribute
echo $file->setAttr('attribute', 'value');
// output: value

// Get all the attributes
print_r ( $file->getAttributes() );

Functional Attributes

registrable

boolean (Default value: true)

Disable the registering of this file (for CSS and JavaScript files only)

// Example

'font-awesome' => [
    // ...
    'css' => [
    	// ...
    	['icons/font-awesome-min.css',
        	// Set false to disable registring file as asset.
        	'registrable'=>false
        ]
    	// ...
    ],
    // ...
],
timestamp

boolean (Default value: false)

Appends file modified time at the end of url. (for CSS and JavaScript no-cdn files only)

// Example

'font-awesome' => [
    // ...
    'css' => [
    	// ...
    	['icons/font-awesome-min.css',
            // output url: ...file.js?v=############ 
        	'timestamp'=>true
        ]
    	// ...
    ],
    // ...
],

Replaceable variables

These variables starts with @ chars and place inside the file name (inside of a string/array's first element. See advance usage on Config Structure wiki page.

@url(*)

* = string

Append a url at the beginning of filename.

// Example
'section' => [
    // ...
    // Example 1: (In a string)
    '@url(http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0)/css/font-awesome.min.css'  
    // output: http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css
    
    // Example 2: (Or inside of array's element)
    ['@url(http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0)/css/font-awesome.min.css'],
    // output: http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css
    // ...
]

@baseUrl

string

Base Url baseUrl property of the [cdn](How to use Yii2cdn#ii-add-a-component) component defined in a @app/config/main.php.

// Example
'font-awesome' => [
    // ...
    'css' => [
    	// ...
    	'@baseUrl/font-awesome/icons/font-awesome-min.css'],
        // output: /cdn/font-awesome/icons/font-awesome-min.css
    	// ...
    ],
    // ...
],

@thisComponentUrl

string

Base Url of it's component.

// Example
'font-awesome' => [
    // ...
    'css' => [
    	// ...
    	'@thisComponentUrl/font-awesome-min.css',
        // output: /cdn/font-awesome/font-awesome-min.css
    	// ...
    ],
    // ...
],

@thisSectionUrl

string

Base Url of it's section.

// Example
'font-awesome' => [
    // ...
    'css' => [
    	// ...
    	'@thisSectionUrl/font-awesome-min.css',
        // output: /cdn/font-awesome/css/font-awesome-min.css
    	// ...
    ],
    // ...
],

@componentUrl(component-id)

component-id = string ID of the component.

Append a cdn component's url at the beginning of filename.

// Example
'bootstrap' => [
    // ...
    'css' => [
    	// ...
    	'css/bootstrap-min.css'
    	// ...
    ],
    // ...
],
'font-awesome' => [
    // ...
    'css' => [
    	// ...
    	'icons/font-awesome-min.css',
        // Include `bootstrap` component's base url
        '@componentUrl(bootstrap)/css/bootstrap-min.css',
        // output: /cdn/bootstrap/css/bootstrap.min.css
    	// ...
    ],
    // ...
],

@componentFile(component-id/section-name/file-id)

component-id = string An ID of the component.

section-name = string A Section name (of the component-id.)

file-id = string A File ID (defined inside the files of section-name of component-id.)

Include a different component's file url.

// Example
'bootstrap' => [
    // ...
    'css' => [
    	// ...
    	['bootstrap-min.css', '@id'=>'core-minified']
    	// ...
    ],
    // ...
],
'font-awesome' => [
    // ...
    'css' => [
    	// ...
    	'icons/font-awesome-min.css',
        // Include `bootstrap` component's base url
        '@componentFile(bootstrap/css/core-minified)',
        // output: /cdn/bootstrap/css/bootstrap.min.css
    	// ...
    ],
    // ...
],

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