Yii2的深入学习--别名(Aliases)
在之前自动加载机制的文章中,我们有提到别名,提到 getAlias 方法,大家当时可能不太清楚,这到底是什么,今天我们就来说一下别名。
别名用来表示文件路径和 URL,这样就避免了将一些文件路径、URL以硬编码的方式写入代码中,或者多处出现一长串的文件路径、URL。
在 Yii2 中,一个别名必须以 @
字符开头,Yii2 预定义了大量可用的别名,预定义的别名如下:
- @yii 表示Yii框架所在的目录,也是 BaseYii.php 文件所在的位置
- @app 表示正在运行的应用的根目录
- @vendor 表示Composer 第三方库所在目录,一般是 @app/vendor 或 @app/../vendor
- @bower 表示 Bower 第三方库所在目录,一般是 @vendor/bower
- @npm 表示 NPM 第三方库所在目录,一般是 @vendor/npm
- @runtime 表示正在运行的应用的运行时用于存放运行时文件的目录,一般是 @app/runtime
- @webroot 表示正在运行的应用的入口文件 index.php 所在的目录,一般是 @app/web
- @web URL别名,表示当前应用的根URL,主要用于前端
- @common 表示通用文件夹
- @frontend 表示前台应用所在的文件夹
- @backend 表示后台应用所在的文件夹
- @console 表示命令行应用所在的文件夹
- 其他使用Composer安装的Yii扩展注册的二级别名
其中的 @common @frontend @backend 和 @console 在 baisc 的项目中是不会存在的。在 advanced 的项目中通常是定义在 common\config\bootstrap.php 文件中,其内容如下:
<span style="color: #000000;">phpYii</span>::setAlias('common', <span style="color: #008080;">dirname</span><span style="color: #000000;">(__DIR__));Yii</span>::setAlias('frontend', <span style="color: #008080;">dirname</span>(<span style="color: #008080;">dirname</span>(__DIR__)) . '/frontend'<span style="color: #000000;">);Yii</span>::setAlias('backend', <span style="color: #008080;">dirname</span>(<span style="color: #008080;">dirname</span>(__DIR__)) . '/backend'<span style="color: #000000;">);Yii</span>::setAlias('console', <span style="color: #008080;">dirname</span>(<span style="color: #008080;">dirname</span>(__DIR__)) . '/console');
Yii2 中关于别名的设置和获取的方法都放在 BaseYii 类中,其结构基本如下:
<span style="color: #000000;">php</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> BaseYii{ </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * @var array registered path aliases * @see getAlias() * @see setAlias() * Yii 的路径别名的 Map, 默认 @yii 指向当前目录 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$aliases</span> = [[email protected]' =><span style="color: #000000;"> __DIR__]; </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * Translates a path alias into an actual path. * 将别名转化为真实的路径 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> getAlias(<span style="color: #800080;">$alias</span>, <span style="color: #800080;">$throwException</span> = <span style="color: #0000ff;">true</span><span style="color: #000000;">) { </span>...<span style="color: #000000;"> } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * Registers a path alias. * 用一个真实的路径注册一个别名 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> setAlias(<span style="color: #800080;">$alias</span>, <span style="color: #800080;">$path</span><span style="color: #000000;">) { </span>...<span style="color: #000000;"> }}</span>
这是简化之后的 BaseYii 类的结构,其中有一个重要的变量 $aliases,两个重要的方法 getAlias 和 setAlias。$aliases 是存储 Yii2 路径别名的一个数组,key 是别名,value 是真实路径。getAlias 方法是根据别名获取到真实的地址,setAlias 是用一个真实的地址去注册一个别名。
先来看下 setAlias 方法,其内容如下:
<span style="color: #008000;">/*</span><span style="color: #008000;">* * Registers a path alias. * * 用一个真实的路径注册一个别名 * * A path alias is a short name representing a long path (a file path, a URL, etc.) * For example, we use [email protected]' as the alias of the path to the Yii framework directory. * * A path alias must start with the character '@' so that it can be easily differentiated * from non-alias paths. * * Note that this method does not check if the given path exists or not. All it does is * to associate the alias with the path. * * Any trailing '/' and '\' characters in the given path will be trimmed. * * @param string $alias the alias name (e.g. "@yii"). It must start with a '@' character. * It may contain the forward slash '/' which serves as boundary character when performing * alias translation by [[getAlias()]]. * @param string $path the path corresponding to the alias. If this is null, the alias will * be removed. Trailing '/' and '\' characters will be trimmed. This can be * * - a directory or a file path (e.g. `/tmp`, `/tmp/main.txt`) * - a URL (e.g. `http://www.yiiframework.com`) * - a path alias (e.g. [email protected]/base`). In this case, the path alias will be converted into the * actual path first by calling [[getAlias()]]. * * @throws InvalidParamException if $path is an invalid alias. * @see getAlias() </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> setAlias(<span style="color: #800080;">$alias</span>, <span style="color: #800080;">$path</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">strncmp</span>(<span style="color: #800080;">$alias</span>, '@', 1<span style="color: #000000;">)) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果不是以 @ 开头,就将 @ 拼到开头</span> <span style="color: #800080;">$alias</span> = '@' . <span style="color: #800080;">$alias</span><span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取 / 在 $alias 中首次出现的位置</span> <span style="color: #800080;">$pos</span> = <span style="color: #008080;">strpos</span>(<span style="color: #800080;">$alias</span>, '/'<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果 / 不存在,$root 就是整个 $alias,否则就是 $alias 中 / 前的内容</span> <span style="color: #800080;">$root</span> = <span style="color: #800080;">$pos</span> === <span style="color: #0000ff;">false</span> ? <span style="color: #800080;">$alias</span> : <span style="color: #008080;">substr</span>(<span style="color: #800080;">$alias</span>, 0, <span style="color: #800080;">$pos</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$path</span> !== <span style="color: #0000ff;">null</span><span style="color: #000000;">) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果 $path 以 @ 开头,使用 getAlias 去获取路径,否则,就去除掉最右边的 /</span> <span style="color: #800080;">$path</span> = <span style="color: #008080;">strncmp</span>(<span style="color: #800080;">$path</span>, '@', 1) ? <span style="color: #008080;">rtrim</span>(<span style="color: #800080;">$path</span>, '\\/') : <span style="color: #0000ff;">static</span>::getAlias(<span style="color: #800080;">$path</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">isset</span>(<span style="color: #0000ff;">static</span>::<span style="color: #800080;">$aliases</span>[<span style="color: #800080;">$root</span><span style="color: #000000;">])) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果不存在这个 $root 的别名</span> <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$pos</span> === <span style="color: #0000ff;">false</span><span style="color: #000000;">) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 没有 /,就将 $path 直接赋值以为 $root 别名对应的路径</span> <span style="color: #0000ff;">static</span>::<span style="color: #800080;">$aliases</span>[<span style="color: #800080;">$root</span>] = <span style="color: #800080;">$path</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 否则,就将 $path 直接赋值为 $root 下的 $alias 的路径</span> <span style="color: #0000ff;">static</span>::<span style="color: #800080;">$aliases</span>[<span style="color: #800080;">$root</span>] = [<span style="color: #800080;">$alias</span> => <span style="color: #800080;">$path</span><span style="color: #000000;">]; } } </span><span style="color: #0000ff;">elseif</span> (<span style="color: #008080;">is_string</span>(<span style="color: #0000ff;">static</span>::<span style="color: #800080;">$aliases</span>[<span style="color: #800080;">$root</span><span style="color: #000000;">])) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果存在,而且是个string类型</span> <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$pos</span> === <span style="color: #0000ff;">false</span><span style="color: #000000;">) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 没有 /,意味着 $alias 就是 $root,直接覆盖即可</span> <span style="color: #0000ff;">static</span>::<span style="color: #800080;">$aliases</span>[<span style="color: #800080;">$root</span>] = <span style="color: #800080;">$path</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 否则,就合并到一起</span> <span style="color: #0000ff;">static</span>::<span style="color: #800080;">$aliases</span>[<span style="color: #800080;">$root</span>] =<span style="color: #000000;"> [ </span><span style="color: #800080;">$alias</span> => <span style="color: #800080;">$path</span>, <span style="color: #800080;">$root</span> => <span style="color: #0000ff;">static</span>::<span style="color: #800080;">$aliases</span>[<span style="color: #800080;">$root</span>],<span style="color: #000000;"> ]; } } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 这种,正常是个 array 类型 // 直接添加进去即可</span> <span style="color: #0000ff;">static</span>::<span style="color: #800080;">$aliases</span>[<span style="color: #800080;">$root</span>][<span style="color: #800080;">$alias</span>] = <span style="color: #800080;">$path</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> krsort — 对数组按照键名逆向排序<br> // 可以做到优先匹配长的别名</span> <span style="color: #008080;">krsort</span>(<span style="color: #0000ff;">static</span>::<span style="color: #800080;">$aliases</span>[<span style="color: #800080;">$root</span><span style="color: #000000;">]); } } </span><span style="color: #0000ff;">elseif</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #0000ff;">static</span>::<span style="color: #800080;">$aliases</span>[<span style="color: #800080;">$root</span><span style="color: #000000;">])) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> $path 为空且对应的别名有值存在,就是要移除相应的别名</span> <span style="color: #0000ff;">if</span> (<span style="color: #008080;">is_array</span>(<span style="color: #0000ff;">static</span>::<span style="color: #800080;">$aliases</span>[<span style="color: #800080;">$root</span><span style="color: #000000;">])) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果 $root 的别名对应一个 array,就只移除掉对应的别名即可</span> <span style="color: #0000ff;">unset</span>(<span style="color: #0000ff;">static</span>::<span style="color: #800080;">$aliases</span>[<span style="color: #800080;">$root</span>][<span style="color: #800080;">$alias</span><span style="color: #000000;">]); } </span><span style="color: #0000ff;">elseif</span> (<span style="color: #800080;">$pos</span> === <span style="color: #0000ff;">false</span><span style="color: #000000;">) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果 $root 的别名对应不是一个 array 而且 $root 就是 $alias,就移除这个 $root 的别名</span> <span style="color: #0000ff;">unset</span>(<span style="color: #0000ff;">static</span>::<span style="color: #800080;">$aliases</span>[<span style="color: #800080;">$root</span><span style="color: #000000;">]); } } }</span>
下面举几个例子来说明,别名写入后,$aliases 中的内容变化。
<span style="color: #008000;">//</span><span style="color: #008000;"> 初始 BaseYii::aliases[[email protected]'] = 'path/to/foo'</span>Yii::setAlias([email protected]', 'path/to/foo'<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;"> 直接覆盖 BaseYii::aliases[[email protected]'] = 'path/to/foo2'</span>Yii::setAlias([email protected]', 'path/to/foo2'<span style="color: #000000;">);</span><span style="color: #008000;">/*</span><span style="color: #008000;">** 新增* BaseYii::aliases[[email protected]'] = [* [email protected]/bar' => 'path/to/foo/bar',* [email protected]' => 'path/to/foo2',* ];</span><span style="color: #008000;">*/</span><span style="color: #000000;">Yii</span>::setAlias([email protected]/bar', 'path/to/foo/bar'<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;"> 初始 BaseYii::aliases[[email protected]'] = [[email protected]/qux' => 'path/to/bar/qux'];</span>Yii::setAlias([email protected]/qux', 'path/to/bar/qux'<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;"> 直接覆盖 BaseYii::aliases[[email protected]'] = [[email protected]/qux' => 'path/to/bar/qux2'];</span>Yii::setAlias([email protected]/qux', 'path/to/bar/qux2'<span style="color: #000000;">);</span><span style="color: #008000;">/*</span><span style="color: #008000;">** 新增* BaseYii::aliases[[email protected]'] = [* [email protected]/foo' => 'path/to/bar/foo',* [email protected]/qux' => 'path/to/bar/qux2',* ];</span><span style="color: #008000;">*/</span><span style="color: #000000;">Yii</span>::setAlias([email protected]/foo', 'path/to/bar/foo'<span style="color: #000000;">);</span><span style="color: #008000;">/*</span><span style="color: #008000;">** 新增* BaseYii::aliases[[email protected]'] = [* [email protected]/foo' => 'path/to/bar/foo',* [email protected]/qux' => 'path/to/bar/qux2',* [email protected]' => 'path/to/bar',* ];</span><span style="color: #008000;">*/</span><span style="color: #000000;">Yii</span>::setAlias([email protected]', 'path/to/bar'<span style="color: #000000;">);</span><span style="color: #008000;">/*</span><span style="color: #008000;">** 删除* BaseYii::aliases[[email protected]'] = [* [email protected]/foo' => 'path/to/bar/foo',* [email protected]' => 'path/to/bar',* ];</span><span style="color: #008000;">*/</span><span style="color: #000000;">Yii</span>::setAlias([email protected]/qux', <span style="color: #0000ff;">null</span><span style="color: #000000;">);</span><span style="color: #008000;">/*</span><span style="color: #008000;">** 删除* BaseYii::aliases[[email protected]'] = [* [email protected]/foo' => 'path/to/bar/foo',* ];</span><span style="color: #008000;">*/</span><span style="color: #000000;">Yii</span>::setAlias([email protected]', <span style="color: #0000ff;">null</span>);
再来看一下 getAlias 方法,其内容如下:
<span style="color: #008000;">/*</span><span style="color: #008000;">* * Translates a path alias into an actual path. * 将别名转化为真实的路径 * * The translation is done according to the following procedure: * * 1. If the given alias does not start with '@', it is returned back without change; * 2. Otherwise, look for the longest registered alias that matches the beginning part * of the given alias. If it exists, replace the matching part of the given alias with * the corresponding registered path. * 3. Throw an exception or return false, depending on the `$throwException` parameter. * * For example, by default [email protected]' is registered as the alias to the Yii framework directory, * say '/path/to/yii'. The alias [email protected]/web' would then be translated into '/path/to/yii/web'. * * If you have registered two aliases [email protected]' and [email protected]/bar'. Then translating [email protected]/bar/config' * would replace the part [email protected]/bar' (instead of [email protected]') with the corresponding registered path. * This is because the longest alias takes precedence. * * However, if the alias to be translated is [email protected]/barbar/config', then [email protected]' will be replaced * instead of [email protected]/bar', because '/' serves as the boundary character. * * Note, this method does not check if the returned path exists or not. * * @param string $alias the alias to be translated. * @param boolean $throwException whether to throw an exception if the given alias is invalid. * If this is false and an invalid alias is given, false will be returned by this method. * @return string|boolean the path corresponding to the alias, false if the root alias is not previously registered. * @throws InvalidParamException if the alias is invalid while $throwException is true. * @see setAlias() </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> getAlias(<span style="color: #800080;">$alias</span>, <span style="color: #800080;">$throwException</span> = <span style="color: #0000ff;">true</span><span style="color: #000000;">) { </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * strncmp — 二进制安全比较字符串开头的若干个字符 * int strncmp ( string $str1 , string $str2 , int $len ) * 如果 $alias 不是以 '@' 开头的,就不是一个 Yii 的别名 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">if</span> (<span style="color: #008080;">strncmp</span>(<span style="color: #800080;">$alias</span>, '@', 1<span style="color: #000000;">)) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> not an alias</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$alias</span><span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取 / 在 $alias 中首次出现的位置</span> <span style="color: #800080;">$pos</span> = <span style="color: #008080;">strpos</span>(<span style="color: #800080;">$alias</span>, '/'<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果 / 不存在,$root 就是整个 $alias,否则就是 $alias 中 / 前的内容</span> <span style="color: #800080;">$root</span> = <span style="color: #800080;">$pos</span> === <span style="color: #0000ff;">false</span> ? <span style="color: #800080;">$alias</span> : <span style="color: #008080;">substr</span>(<span style="color: #800080;">$alias</span>, 0, <span style="color: #800080;">$pos</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果存在 $root 的别名</span> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #0000ff;">static</span>::<span style="color: #800080;">$aliases</span>[<span style="color: #800080;">$root</span><span style="color: #000000;">])) { </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">is_string</span>(<span style="color: #0000ff;">static</span>::<span style="color: #800080;">$aliases</span>[<span style="color: #800080;">$root</span><span style="color: #000000;">])) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果 $root 对应的别名是一个字符串,之直接返回 $aliases[$root] 或者 $aliases[$root] . substr($alias, $pos) // 当 $root 就是 $alias 返回 $aliases[$root], 否则就在拼接上 $alias 除去 $root 后,剩下的字符串</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$pos</span> === <span style="color: #0000ff;">false</span> ? <span style="color: #0000ff;">static</span>::<span style="color: #800080;">$aliases</span>[<span style="color: #800080;">$root</span>] : <span style="color: #0000ff;">static</span>::<span style="color: #800080;">$aliases</span>[<span style="color: #800080;">$root</span>] . <span style="color: #008080;">substr</span>(<span style="color: #800080;">$alias</span>, <span style="color: #800080;">$pos</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 否则,要遍历整个 $aliases[$root] 数组,找到 $name 与 $alias 相同的值,返回 $path . substr($alias, strlen($name)) // 其实是返回了 $path 拼接上 $alias 除去 $root 后,剩下的字符串</span> <span style="color: #0000ff;">foreach</span> (<span style="color: #0000ff;">static</span>::<span style="color: #800080;">$aliases</span>[<span style="color: #800080;">$root</span>] <span style="color: #0000ff;">as</span> <span style="color: #800080;">$name</span> => <span style="color: #800080;">$path</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">strpos</span>(<span style="color: #800080;">$alias</span> . '/', <span style="color: #800080;">$name</span> . '/') === 0<span style="color: #000000;">) { </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$path</span> . <span style="color: #008080;">substr</span>(<span style="color: #800080;">$alias</span>, <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$name</span><span style="color: #000000;">)); } } } } </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$throwException</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> InvalidParamException("Invalid path alias: <span style="color: #800080;">$alias</span>"<span style="color: #000000;">); } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">; } }</span>
好了,关于别名就先说这么多~~
对 Yii2 源码有兴趣的同学可以关注项目 yii2-2.0.3-annotated,现在在上面已经添加了不少关于 Yii2 源码的注释,之后还会继续添加~
有兴趣的同学也可以参与进来,提交 Yii2 源码的注释。

一封电子邮件的旅程是:MUA:MailUserAgent——邮件用户代理。(即类似Outlook的电子邮件软件)MTA:MailTransferAgent——邮件传输代理,就是那些Email服务提供商,比如网易、新浪等等。MDA:MailDeliveryAgent——邮件投递代理。Email服务提供商的某个服务器发件人->MUA->MTA->MTA->若

root和alias都可以定义在location模块中,都是用来指定请求资源的真实路径,比如:location/i/{root/data/w3;}请求http://foofish.net/i/top.gif这个地址时,那么在服务器里面对应的真正的资源是/data/w3/i/top.gif文件注意:真实的路径是root指定的值加上location指定的值。而alias正如其名,alias指定的路径是location的别名,不管location的值怎么写,资源的真实路径都是alias指定的路径,比如

Linux系统如何设置PATH环境变量在Linux系统中,PATH环境变量用于指定系统在命令行中搜索可执行文件的路径。正确设置PATH环境变量可以方便我们在任何位置执行系统命令和自定义命令。本文将介绍如何在Linux系统中设置PATH环境变量,并提供详细的代码示例。查看当前的PATH环境变量在终端中执行以下命令,可以查看当前的PATH环境变量:echo$P

设置path环境变量的方法:1、Windows系统,打开“系统属性”,点击“属性”选项,点击“高级系统设置”,在“系统属性”窗口中,选择“高级”标签,然后点击“环境变量”按钮,找到并点击“Path”编辑保存后即可;2、Linux系统,打开终端,打开你的bash配置文件,在文件末尾添加“export PATH=$PATH:文件路径”保存即可;3、MacOS系统,操作同上。

如何正确设置Linux中的PATH环境变量在Linux操作系统中,环境变量是用来存储系统级别的配置信息的重要机制之一。其中,PATH环境变量被用来指定系统在哪些目录中查找可执行文件。正确设置PATH环境变量是确保系统正常运行的关键一步。本文将介绍如何正确设置Linux中的PATH环境变量,并提供具体的代码示例。1.查看当前PATH环境变量在终端中输入以下命

《Linux中PATH环境变量的作用和重要性》PATH环境变量是Linux系统中非常重要的环境变量之一,它定义了系统在哪些目录中寻找可执行程序。在Linux系统中,当用户在终端输入一个命令时,系统会在PATH环境变量所列出的目录中逐个查找是否存在该命令的可执行文件,如果找到则执行,否则会提示“commandnotfound”。PATH环境变量的作用:简化

配置步骤:1、找到Java安装目录;2、找到系统的环境变量设置;3、在环境变量窗口中,找到名为“Path”的变量,并点击编辑按钮;4、在弹出的编辑环境变量窗口中,点击“新建”按钮,并在弹出的对话框中输入Java的安装路径;5、确认输入正确后,点击“确定”按钮即可。

alias命令用来设置指令的别名。我们可以使用该命令可以将一些较长的命令进行简化。alias命令的作用只局限于该次登入的操作,下面我们就来看看详细的教程。1、语法格式:alias[参数]比如我们给操作:cat命令起一个别名hhcat,命令为:aliashhcat=cat2、当我们使用cat命令的时候就等同于hhcat命令3、可以使用参数-p打印已经设置的命令别名4、若要取消一个命令别名,则是用unalias命令,格式为“unalias别名”,我们取消刚才的hhcat别名,命令为:u


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

禪工作室 13.0.1
強大的PHP整合開發環境