Android Source Learning yii2 Source Learning Note 17)

Source: Internet
Author: User
Tags throw exception yii
Theme class, applied theme, implement theme by replacing path, method to get root path and root link: yii2\base\theme.php

1 
 Php2/**3* @linkhttp://www.yiiframework.com/4* @copyright Copyright (c) Yii software LLC5* @licensehttp://www.yiiframework.com/license/6*/78namespaceYii\Base;9TenUse Yii; OneUse yii\helpers\filehelper; A -/** -* Theme represents an application Theme. the* Theme class, applied theme -* When [[View]] renders a View file, it'll check the [[View::theme|active theme] -* to-see if there is a themed version of the view file exists. If So, the themed version would be rendered instead. -* When the View object [[view]] Renders the view file, it checks to see if the theme of the view exists and renders the theme instead of the default style if it exists +* A theme is a directory consisting of view files which be meant to replace their non-themed counterparts. - * +* Theme uses [[Pathmap]] to achieve the view file replacement: A * at* 1. It first looks for a key in [[Pathmap]], which is a substring of the given view file path; -* First find the keyword, the keyword is a given view path of the string -* 2. If such a key exists, the corresponding value would be used to replace the corresponding part -* In the view file path; If the keyword exists, replace the corresponding part in the path of the given view files with the corresponding value -* 3. It would then check if the updated view file exists or not. If So, then file would be used -* To replace the original view file. Check for the existence of files that correspond to the replaced path, replace the original file in* 4. If Step 2 or 3 fails, the original view file would be used. -* 2 and 3 fail, return to the original path to* For example, if [[Pathmap]] is ' [' @app/views ' = ' @app/themes/basic '] ', +* Then the themed version for a view file ' @app/views/site/index.php ' 'll be -* ' @app/themes/basic/site/index.php '. the * ** It is possible to map a single path to multiple paths. For example, $ *Panax Notoginseng * ~~~ -* ' pathmap ' = [ the* ' @app/views ' = [ +* ' @app/themes/christmas ', A* ' @app/themes/basic ', the *     ], + * ] - * ~~~ $ * $the themed version could be either ' @app/themes/christmas/site/index.php ' or -* ' @app/themes/basic/site/index.php '. The former have precedence over the latter if both files exist. - * the* To use a theme, you should configure the [[View::theme|theme]] property of the "View" Application -* component like the following:Wuyi * the * ~~~ -* ' View ' = [ Wu* ' Theme ' = [ -* ' basepath ' = ' @app/themes/basic ', About* ' baseUrl ' = ' @web/themes/basic ', $ *     ], - * ], - * ~~~ - * A* The above configuration specifies a theme located under the "Themes/basic" directory of the Web folder +* that contains the entry script of the application. If your theme is designed to handle modules, the* Configure the [[Pathmap]] property like described above. - * $* @property string $basePath The root path of this theme. All resources of this theme is located under this the* directory. the* @property string $BASEURL the base URL (without ending slash) for this theme. All resources of this theme the* is considered to is under this base URL. This property is read-only. the * -* @author Qiang Xue 
     in* @since 2.0 the*/ theclassTheme extends Component About{ the/** the* @var Array The mapping between view directories and their corresponding themed versions. the* This property was used by [[ApplyTo ()]] When a view was trying to apply the theme. +* Path aliases can used when specifying directories. -* Path Map Property Set replace mapping relationship the* If The property was empty or not set, a mapping [[Application::basepath]] to [[BasePath]] would be used.Bayi*/ the Public$pathMap; the -Private$_baseurl;//set the URL to access a resource - the/** the* @return String The base URL (without ending slash) for this theme. All resources of this theme is considered the* To is under the this base URL. Returns the underlying link to the current topic, with all other resources in the link the*/ - Publicfunction Getbaseurl () the    { thereturn$ This-_baseurl; the    }94 the/** the* @param $url string The base URL or path alias for this theme. All resources of this theme is considered the* To is under this base URL. Setting the underlying link98*/ About Publicfunction Setbaseurl ($url) -    {101$ This->_baseurl = RTrim (Yii::getalias ($url),'/');102    }103104Private$_basepath;//Root Path the106/**107* @return String The root path of this theme. All resources of this theme is located under this directory.108* Get the root path of the current topic109* @see Pathmap the*/111 Publicfunction Getbasepath () the    {113return$ This-_basepath; the    } the the/**117* @param string $path The root path or path alias of this theme. All resources of this theme is located118* Under this directory. Set the current theme root path119* @see Pathmap -*/121 Publicfunction Setbasepath ($path)122    {123$ This->_basepath =Yii::getalias ($path);124    } the126/**127* Converts a files to a themed file if possible. Replace a document with a theme file -* If There is no corresponding themed file, the original file would be returned.129* No corresponding theme file, return the original file.  the* @param string $path The file to be themed131* @return string The themed file, or the original file if the themed version is not available. the* @throws invalidconfigexception if [[BasePath]] is not set133*/134 Publicfunction ApplyTo ($path)135    {136$pathMap = $ This->pathmap;//Get path mapping137if(Empty ($PATHMAP)) {//No setting value throws an exception138if(($basePath = $ This->getbasepath ()) = = =NULL) {139ThrowNewInvalidconfigexception ('the "BasePath" property must is set.'); $            }141//set value as [module root path = = Theme Root Path] form142$pathMap = [Yii:: $app->getbasepath () =[$basePath]];143        }144145$path = Filehelper::normalizepath ($path);//The "/" in the path. \ "to unify146147foreach($pathMap as$ from=$tos) {148//mapping the source in an array149$ from= Filehelper::normalizepath (Yii::getalias ($ from)) . Directory_separator; Maxif(Strpos ($path, $ from) ===0) {//If there are old values that can be replaced in the $path151$n = strlen ($ from); theforeach(array) $tos as$to) {153$to =Filehelper::normalizepath (Yii::getalias ($to)). Directory_separator;154$file = $to. substr ($path, $n);//replace $from in $path with $to155if(Is_file ($file)) {156return$file;//is the file returned directly157                    }158                }159            } the        }161162return$path;163    }164165/**166* Converts a relative URL to an absolute URL using [[BASEURL]].167* Convert a relative URL to an absolute URL168* @param string $url the relative URL to being converted. Relative URL to convert169* @return string The absolute URL after the absolute URL is replaced the* @throws invalidconfigexception if [[BASEURL]] is not set171*/172 Publicfunction GetUrl ($url)173    {174if(($BASEURL = $ This->getbaseurl ())!==NULL) {//URL exists, for conversion175return$baseUrl.'/'. LTrim ($url,'/');176}Else{//There is no throw exception177ThrowNewInvalidconfigexception ('the "BaseUrl" property must is set.');178        }179    } the181/**182* Converts a relative file path to an absolute one using [[BasePath]].183* Generating absolute paths from relative paths184* @param string $path The relative file path to be converted. relative files to convert. 185* @return String The absolute path after the absolute file path is converted186* @throws invalidconfigexception if [[BASEURL]] is not set187*/188 Publicfunction GetPath ($path)189    { theif(($basePath = $ This->getbasepath ())!==NULL) {191//returns the path of the stitching192return$basePath. Directory_separator. LTrim ($path,'/\\');193}Else {194ThrowNewInvalidconfigexception ('the "BasePath" property must is set.');195        }196    }197}

The above describes the Android source learning yii2 Source Learning Note 17, including the Android source learning content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.