


Detailed explanation of the use of the front-end resource package that comes with PHP's Yii framework, yii framework_PHP tutorial
Jul 12, 2016 am 08:55 AMDetailed explanation of the use of the front-end resource package that comes with PHP's Yii framework. The yii framework
The resources in Yii are files related to the Web page, which can be CSS files, JavaScript Files, pictures or videos, etc., resources are placed in a directory accessible to the Web and are directly called by the Web server.
It is better to automatically manage resources through programs. For example, when you use the yiijuiDatePicker widget in a page, it will automatically include the required CSS and JavaScript files, instead of requiring you to manually find these files and include them. When you upgrade a widget, it will automatically use the new version of the resource file. In this tutorial, we will detail the powerful resource management functions provided by Yii.
Resource Pack
Yii manages resources in resource packages. Resource packages are simply a collection of resources placed in a directory. When a resource package is registered in a view, the CSS and JavaScript files in the package will be included when rendering the web page.
Define resource package
The resource package is designated as a PHP class that inherits yiiwebAssetBundle. The package name is a PHP class name that can be automatically loaded. In the resource package class, you need to specify the location of the resource, which CSS and JavaScript files it contains, and its dependencies with other packages.
The following code defines the main resource package used by the basic application template:
<?php namespace app\assets; use yii\web\AssetBundle; class AppAsset extends AssetBundle { public $basePath = '@webroot'; public $baseUrl = '@web'; public $css = [ 'css/site.css', ]; public $js = [ ]; public $depends = [ 'yii\web\YiiAsset', 'yii\bootstrap\BootstrapAsset', ]; }
As above, the resource file specified by the AppAsset class is placed in the @webroot directory, and the corresponding URL is @web. The resource package contains a CSS file css/site.css, no JavaScript file, and relies on the other two packages yiiwebYiiAsset and yiibootstrapBootstrapAsset. About More details on the properties of yiiwebAssetBundle are described below:
- yiiwebAssetBundle::sourcePath: Specifies the root directory of the bundle containing resource files. This property should be set when the root directory cannot be accessed by the Web. Otherwise, the yiiwebAssetBundle::basePath property and yiiwebAssetBundle::baseUrl should be set. Path aliases can be used here;
- yiiwebAssetBundle::basePath: Specifies the directory that contains the resource files in the resource bundle and is Web-accessible. When specifying the yiiwebAssetBundle::sourcePath attribute, the resource manager will publish the package's resources to a Web-accessible directory and override this attribute. If you If the resource file is in a Web-accessible directory, this attribute should be set so that it does not need to be published again. Path aliases can be used here.
yiiwebAssetBundle::baseUrl: Specifies the URL corresponding to the yiiwebAssetBundle::basePath directory. Similar to yiiwebAssetBundle::basePath, if you specify the yiiwebAssetBundle::sourcePath attribute, the resource manager will publish these resources and override this attribute. The path alias can be used here.
yiiwebAssetBundle::js: An array containing the JavaScript files of the resource bundle. Note that the forward slash "/" should be used as the directory separator. Each JavaScript file can be specified in one of the following two formats:
- The relative path is represented as a local JavaScript file (such as js/main.js). The actual path of the file is preceded by yiiwebAssetManager::basePath. The actual URL of the file is preceded by yiiwebAssetManager::baseUrl.
- Absolute URL addresses are represented as external JavaScript files, such as http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js or //ajax.googleapis.com/ajax/libs /jquery/2.1.1/jquery.min.js.
- yiiwebAssetBundle::css: An array containing the JavaScript files of the resource bundle. The array format is the same as yiiwebAssetBundle::js.
- yiiwebAssetBundle::depends: A list of other resource bundles that this resource bundle depends on (detailed introduction in the next two sections).
- yiiwebAssetBundle::jsOptions: When calling yiiwebView::registerJsFile() to register each JavaScript file in the bundle, specify the options passed to this method.
- yiiwebAssetBundle::cssOptions: When calling yiiwebView::registerCssFile() to register each css file in the bundle, specify the options passed to this method.
- yiiwebAssetBundle::publishOptions: When calling yiiwebAssetManager::publish() to publish the package resource file to the Web directory, specify the options passed to this method, only used when the yiiwebAssetBundle::sourcePath attribute is specified.
Resource location
Resources can be divided according to their location into:
Source resources: Resource files and PHP source code are put together and cannot be directly accessed by the Web. In order to use these source resources, they must be copied to a Web directory that can be accessed by the Web and become published resources. This process is called publishing. Resources will be introduced in detail later.
Publish resources: Resource files are placed in a Web directory that can be accessed directly through the Web;
External resources: Resource files are placed on a different web server than your web application;
When defining the resource bundle class, if you specify the yiiwebAssetBundle::sourcePath attribute, it means that any resources using relative paths will be used as source resources; if you do not specify this attribute, it means that these resources are published resources (therefore you should specify yiiwebAssetBundle ::basePath and yiiwebAssetBundle::baseUrl let Yii know their location).
It is recommended to place resource files in the web directory to avoid unnecessary resource publishing process. This is why the previous example specifies yiiwebAssetBundle::basePath instead of yiiwebAssetBundle::sourcePath.
對(duì)于 擴(kuò)展來(lái)說(shuō),由于它們的資源和源代碼都在不能Web訪問(wèn)的目錄下, 在定義資源包類時(shí)必須指定yii\web\AssetBundle::sourcePath屬性。
注意: yii\web\AssetBundle::sourcePath 屬性不要用@webroot/assets,該路徑默認(rèn)為 yii\web\AssetManager資源管理器將源資源發(fā)布后存儲(chǔ)資源的路徑,該路徑的所有內(nèi)容會(huì)認(rèn)為是臨時(shí)文件, 可能會(huì)被刪除。
資源依賴
當(dāng)Web頁(yè)面包含多個(gè)CSS或JavaScript文件時(shí),它們有一定的先后順序以避免屬性覆蓋, 例如,Web頁(yè)面在使用jQuery UI小部件前必須確保jQuery JavaScript文件已經(jīng)被包含了, 我們稱這種資源先后次序稱為資源依賴。
資源依賴主要通過(guò)yii\web\AssetBundle::depends 屬性來(lái)指定, 在AppAsset 示例中,資源包依賴其他兩個(gè)資源包: yii\web\YiiAsset 和 yii\bootstrap\BootstrapAsset 也就是該資源包的CSS和JavaScript文件要在這兩個(gè)依賴包的文件包含 之后 才包含。
資源依賴關(guān)系是可傳遞,也就是人說(shuō)A依賴B,B依賴C,那么A也依賴C。
資源選項(xiàng)
可指定yii\web\AssetBundle::cssOptions 和 yii\web\AssetBundle::jsOptions 屬性來(lái)自定義頁(yè)面包含CSS和JavaScript文件的方式, 這些屬性值會(huì)分別傳遞給 yii\web\View::registerCssFile() 和 yii\web\View::registerJsFile() 方法, 在視圖 調(diào)用這些方法包含CSS和JavaScript文件時(shí)。
注意: 在資源包類中設(shè)置的選項(xiàng)會(huì)應(yīng)用到該包中 每個(gè) CSS/JavaScript 文件,如果想對(duì)每個(gè)文件使用不同的選項(xiàng), 應(yīng)創(chuàng)建不同的資源包并在每個(gè)包中使用一個(gè)選項(xiàng)集。
例如,只想IE9或更高的瀏覽器包含一個(gè)CSS文件,可以使用如下選項(xiàng):
public $cssOptions = ['condition' => 'lte IE9'];
這會(huì)是包中的CSS文件使用以下HTML標(biāo)簽包含進(jìn)來(lái):
<!--[if lte IE9]> <link rel="stylesheet" href="path/to/foo.css"> <![endif]-->
為鏈接標(biāo)簽包含

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

TopreventCSRFattacksinPHP,implementanti-CSRFtokens.1)Generateandstoresecuretokensusingrandom_bytes()orbin2hex(random_bytes(32)),savethemin$_SESSION,andincludetheminformsashiddeninputs.2)ValidatetokensonsubmissionbystrictlycomparingthePOSTtokenwiththe

To merge two PHP arrays and keep unique values, there are two main methods. 1. For index arrays or only deduplication, use array_merge and array_unique combinations: first merge array_merge($array1,$array2) and then use array_unique() to deduplicate them to finally get a new array containing all unique values; 2. For associative arrays and want to retain key-value pairs in the first array, use the operator: $result=$array1 $array2, which will ensure that the keys in the first array will not be overwritten by the second array. These two methods are applicable to different scenarios, depending on whether the key name is retained or only the focus is on

exit() is a function in PHP that is used to terminate script execution immediately. Common uses include: 1. Terminate the script in advance when an exception is detected, such as the file does not exist or verification fails; 2. Output intermediate results during debugging and stop execution; 3. Call exit() after redirecting in conjunction with header() to prevent subsequent code execution; In addition, exit() can accept string parameters as output content or integers as status code, and its alias is die().

The rational use of semantic tags in HTML can improve page structure clarity, accessibility and SEO effects. 1. Used for independent content blocks, such as blog posts or comments, it must be self-contained; 2. Used for classification related content, usually including titles, and is suitable for different modules of the page; 3. Used for auxiliary information related to the main content but not core, such as sidebar recommendations or author profiles. In actual development, labels should be combined and other, avoid excessive nesting, keep the structure simple, and verify the rationality of the structure through developer tools.

To access session data in PHP, you must first start the session and then operate through the $_SESSION hyperglobal array. 1. The session must be started using session_start(), and the function must be called before any output; 2. When accessing session data, check whether the key exists. You can use isset($_SESSION['key']) or array_key_exists('key',$_SESSION); 3. Set or update session variables only need to assign values ??to the $_SESSION array without manually saving; 4. Clear specific data with unset($_SESSION['key']), clear all data and set $_SESSION to an empty array.

Recursive functions refer to self-call functions in PHP. The core elements are 1. Defining the termination conditions (base examples), 2. Decomposing the problem and calling itself recursively (recursive examples). It is suitable for dealing with hierarchical structures, disassembling duplicate subproblems, or improving code readability, such as calculating factorials, traversing directories, etc. However, it is necessary to pay attention to the risks of memory consumption and stack overflow. When writing, the exit conditions should be clarified, the basic examples should be gradually approached, the redundant parameters should be avoided, and small inputs should be tested. For example, when scanning a directory, the function encounters a subdirectory and calls itself recursively until all levels are traversed.

When you encounter the prompt "This operation requires escalation of permissions", it means that you need administrator permissions to continue. Solutions include: 1. Right-click the "Run as Administrator" program or set the shortcut to always run as an administrator; 2. Check whether the current account is an administrator account, if not, switch or request administrator assistance; 3. Use administrator permissions to open a command prompt or PowerShell to execute relevant commands; 4. Bypass the restrictions by obtaining file ownership or modifying the registry when necessary, but such operations need to be cautious and fully understand the risks. Confirm permission identity and try the above methods usually solve the problem.

The way to process raw POST data in PHP is to use $rawData=file_get_contents('php://input'), which is suitable for receiving JSON, XML, or other custom format data. 1.php://input is a read-only stream, which is only valid in POST requests; 2. Common problems include server configuration or middleware reading input streams, which makes it impossible to obtain data; 3. Application scenarios include receiving front-end fetch requests, third-party service callbacks, and building RESTfulAPIs; 4. The difference from $_POST is that $_POST automatically parses standard form data, while the original data is suitable for non-standard formats and allows manual parsing; 5. Ordinary HTM
