Analysis on the usage of setAlias and getAlias functions in Yii2, and yii2setalias
This example describes how to set and obtain aliases (setAlias and getAlias) in yii2. We will share this with you for your reference. The details are as follows:
First, let's talk aboutWhat is an alias?.
There are many paths in Yii, and some paths will be used during the development process. In general, absolute paths are required, but absolute paths are long.
Therefore, to facilitate the use of the path, you can give each path a name in Yi, which is the alias.
Alias format:
The alias must start with "@" and can contain "/". For example, ("@ www" is the root alias and "@ www/test" is the sub-alias)
The directory separator ("\" or "/") at the end of the alias will be removed (if any)
The path of an alias is in the following format:
Directory path or file path ('/tmp', '/tmp/main.txt ')
URL ('HTTP: // www.yii2.com ')
Another alias path ('@ yii/base') will call getAlias () to parse the alias.
Set alias:
Yii::setAlias("@www", "C:\www\");Yii::setAlias("@data", "C:\www\data");Yii::setAlias("@data/attach", "C:\www\data\attach\icon.png");Yii::setAlias("@home", "www.yiifans.com\");Yii::setAlias("@admin", "@www\admin");
When setting an alias, if the path contains an alias, getAlias () is called to parse the alias.
For example, if the path of the alias @ yii is/xxx/yii, @ yii/base will be resolved to/xxx/yii/base
Get alias:
Obtain the alias path in the following order:
If the alias does not start with "@", the alias is directly returned and not processed.
Find the longest matching alias, and replace the alias in the input parameter with the alias path.
For example, you have set two aliases: "@ foo" => "my" and "@ foo/test" => "my/test ".
If @ foo/test/config is used to obtain the corresponding path, the result is "my/test/config"
If @ foo/testtst/config is used to obtain the corresponding path, the result is "my/testtst/config"
If @ foo/config is used to obtain the corresponding path, the result is "my/config"
That is, "/" is used as the separator to match the longest alias.
The alias settings above are as follows:
Yii: getAlias ("@ www"); // output: C: \ wwwYii: getAlias ("@ data"); // output: C: \ www \ dataYii:: getAlias ("@ data/attach"); // output: C: \ www \ data \ attach \ icon.png Yii: getAlias ("@ home"); // output: www. yiifans. comYii: getAlias ("@ admin"); // output: C: \ www \ admin
Set the alias function source code:SetAlias
Public static function setAlias ($ alias, $ path) {if (strncmp ($ alias, '@', 1) {// if it does not start, then @ $ alias = '@' is automatically added to the front '@'. $ alias;} // find "/" $ pos = strpos ($ alias, '/') in the alias; // If the alias does not contain "/", the root alias is the input alias. Otherwise, the root alias is @ www, for example, @ www, before "/" is intercepted. // For example: @ www/data, the root alias is intercepted as @ www. $ Root = $ pos = false? $ Alias: substr ($ alias, 0, $ pos); if ($ path! = Null) {/** if the path contains an alias, call getAlias to parse the path. * Otherwise, remove the ending "/", "\" */$ path = strncmp ($ path, '@', 1 )? Rtrim ($ path, '\/'): static: getAlias ($ path); if (! Isset (static: $ aliases [$ root]) {/** if you have not set this root alias (@ www) ** if the alias is the root alias (the parameter is @ www), you can directly set the path (['@ www' => 'path']) for the alias. * otherwise, the generated path is an array (['@ www' => ['@ www/data' => 'path']) ** @ www ['@ www' => 'xxxx'] * @ www/a ['@ www' => ['@ www/A' => xxxa] * @ www/B ['@ www' => ['@ www/B' => xxxb] * @ www/a/B ['@ www' => ['@ www/a/B '=> xxxc] ** note: only one of the four above will be registered, because I have determined whether to register @ www */if ($ pos = false) {static :: $ aliases [$ root] = $ path;} else {Static: $ aliases [$ root] = [$ alias => $ path] ;}} elseif (is_string (static ::$ aliases [$ root]) {/** the root alias has been registered (only the root alias has been registered, and the corresponding value will be a string) ** if the root alias is currently registered, the old value will be overwritten. * ['@ Www'] = xxx ** otherwise, add the current alias and root alias to the root alias array * @ www/a ['@ www'] = ['@ www /A' => xxxa, '@ www' => 'xxx'] **/if ($ pos = false) {static: $ aliases [$ root] = $ path ;} else {static ::$ aliases [$ root] = [$ alias => $ path, $ root => static ::$ aliases [$ root],];} else {// Add to the array of the root alias static: $ aliases [$ root] [$ alias] = $ path; krsort (static :: $ aliases [$ root]) ;}} elseif (isset (static: $ aliases [$ root]) {// if it is a root alias array, delete the sub-alias. If (is_array (static: $ aliases [$ root]) {unset (static: $ aliases [$ root] [$ alias]);} elseif ($ pos = false) {// Delete the entire root alias array unset (static: $ aliases [$ root]);}
For example:
@ Www => xxx ['@ www'] = xxx
@ Www => yyy ['@ www'] = yyy
@ Www/a => yyya ['@ www'] = ['@ www/A' => yyya,' @ www '=> yyy]
@ Www/B = yyyb ['@ www'] = ['@ www/A' => yyya,' @ www '=> yyy, '@ www/B' => yyyb]
That is to say, all data with the same root alias will be put in an array with the root alias as the key, and the key isReverse sorting
Obtain the alias function source code:GetAlias
Public static function getAlias ($ alias, $ throwException = true) {// if it does not start with @, alias if (strncmp ($ alias, '@', 1) is returned directly )) {// not an alias return $ alias;} // obtain the root alias, which is the same as setAlias $ pos = strpos ($ alias ,'/'); $ root = $ pos = false? $ Alias: substr ($ alias, 0, $ pos); if (isset (static: $ aliases [$ root]) {// if the root alias value is a string, that is, only one root alias (['@ www' => xxx]) if (is_string (static: $ aliases [$ root]) is set. {/** if the obtained name is the root alias, the corresponding path of the root alias is directly returned. * otherwise, the returned value is the root alias path + path after the alias is removed. ** example: * @ www, directly return xxx * @ www/aaa/bbb and return xxx/aaa/bbb */return $ pos = false? Static: $ aliases [$ root]: static: $ aliases [$ root]. substr ($ alias, $ pos );} else {/* traverse sub-alias ** @ www => xxx ['@ www'] = xxx * @ www => yyy ['@ www'] = yyy * @ www/ a => yyya ['@ www'] = ['@ www/A' => yyya, '@ www' => yyy] * @ www/B => yyyb ['@ www'] = ['@ www/A' => yyya, '@ www/B' => yyyb, '@ www' => yyy] * @ www/a/c => yyyac ['@ www'] = ['@ www/a/C' => yyyac, '@ www/A' => yyya,' @ www/B '=> yyyb,' @ www '=> yyy] */foreach (static :: $ aliases [$ root] as $ name => $ Path) {/** here, because all the sub-aliases of the root alias are sorted in reverse order based on the key, that is, the sub-alias *, the longest alias is always matched during search. ** Because "/" is added to the alias during comparison, "/" can be used as the separator during search. */If (strpos ($ alias. '/', $ name. '/') = 0) {return $ path. substr ($ alias, strlen ($ name) ;}}}if ($ throwException) {throw new InvalidParamException ("Invalid path alias: $ alias ");} else {return false ;}}