🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 常用类 ``` <pre class="calibre19">``` 生成url use yii\helpers\Url<span class="token2">;</span> $url <span class="token">=</span> Url<span class="token2">:</span><span class="token2">:</span><span class="token1">to</span><span class="token2">(</span><span class="token2">[</span><span class="token4">'post/view'</span><span class="token2">,</span> <span class="token4">'id'</span> <span class="token">=</span><span class="token">></span> <span class="token3">100</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> Url<span class="token2">:</span><span class="token2">:</span><span class="token1">to</span><span class="token2">(</span><span class="token2">[</span><span class="token4">'post/view'</span><span class="token2">,</span> <span class="token4">'id'</span> <span class="token">=</span><span class="token">></span> <span class="token3">100</span><span class="token2">,</span> <span class="token4">'#'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'content'</span><span class="token2">]</span><span class="token2">,</span><span class="token3">true</span><span class="token2">)</span><span class="token2">;</span> 带域名http<span class="token2">:</span><span class="token">/</span><span class="token">/</span>yii<span class="token2">.</span>api<span class="token2">.</span>shanliwawa<span class="token2">.</span>top<span class="token2">:</span><span class="token3">80</span><span class="token">/</span>post<span class="token">/</span>view<span class="token">?</span>id<span class="token">=</span><span class="token3">100</span>#content <span class="token6">// 主页URL:/index.php?r=site%2Findex</span> echo Url<span class="token2">:</span><span class="token2">:</span><span class="token1">home</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">// 根URL,如果程序部署到一个Web目录下的子目录时非常有用</span> echo Url<span class="token2">:</span><span class="token2">:</span><span class="token1">base</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> ``` ``` 请求 ``` <pre class="calibre14">``` 为空时表示全部 $request <span class="token">=</span> Yii<span class="token2">:</span><span class="token2">:</span>$app<span class="token">-</span><span class="token">></span>request<span class="token2">;</span> $get <span class="token">=</span> $request<span class="token">-</span><span class="token">></span><span class="token1">get</span><span class="token2">(</span><span class="token4">'id'</span><span class="token2">)</span><span class="token2">;</span> $id <span class="token">=</span> $request<span class="token">-</span><span class="token">></span><span class="token1">get</span><span class="token2">(</span><span class="token4">'id'</span><span class="token2">,</span> <span class="token3">1</span><span class="token2">)</span><span class="token2">;</span> 不存在为<span class="token3">1</span> $name <span class="token">=</span> $request<span class="token">-</span><span class="token">></span><span class="token1">post</span><span class="token2">(</span><span class="token4">'name'</span><span class="token2">)</span><span class="token2">;</span> api中 $params <span class="token">=</span> $request<span class="token">-</span><span class="token">></span>bodyParams<span class="token2">;</span> <span class="token6">// 返回参数 "id"</span> $param <span class="token">=</span> $request<span class="token">-</span><span class="token">></span><span class="token1">getBodyParam</span><span class="token2">(</span><span class="token4">'id'</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span>$request<span class="token">-</span><span class="token">></span>isAjax<span class="token2">)</span> <span class="token2">{</span> <span class="token6">/* 该请求是一个 AJAX 请求 */</span> <span class="token2">}</span> <span class="token5">if</span> <span class="token2">(</span>$request<span class="token">-</span><span class="token">></span>isGet<span class="token2">)</span> <span class="token2">{</span> <span class="token6">/* 请求方法是 GET */</span> <span class="token2">}</span> <span class="token5">if</span> <span class="token2">(</span>$request<span class="token">-</span><span class="token">></span>isPost<span class="token2">)</span> <span class="token2">{</span> <span class="token6">/* 请求方法是 POST */</span> <span class="token2">}</span> <span class="token5">if</span> <span class="token2">(</span>$request<span class="token">-</span><span class="token">></span>isPut<span class="token2">)</span> <span class="token2">{</span> <span class="token6">/* 请求方法是 PUT */</span> <span class="token2">}</span> $userHost <span class="token">=</span> Yii<span class="token2">:</span><span class="token2">:</span>$app<span class="token">-</span><span class="token">></span>request<span class="token">-</span><span class="token">></span>userHost<span class="token2">;</span>客户主机 $userIP <span class="token">=</span> Yii<span class="token2">:</span><span class="token2">:</span>$app<span class="token">-</span><span class="token">></span>request<span class="token">-</span><span class="token">></span>userIP<span class="token2">;</span>客户ip ``` ``` 跳转 ``` <pre class="calibre14">``` yii\web\Response<span class="token2">:</span><span class="token2">:</span><span class="token1">redirect</span><span class="token2">(</span>$url<span class="token2">)</span> $this<span class="token">-</span><span class="token">></span><span class="token1">redirect</span><span class="token2">(</span><span class="token4">'http://example.com/new'</span><span class="token2">,</span> <span class="token3">301</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//控制器中</span> ``` ``` session ``` <pre class="calibre14">``` $session <span class="token">=</span> Yii<span class="token2">:</span><span class="token2">:</span>$app<span class="token">-</span><span class="token">></span>session<span class="token2">;</span> <span class="token6">// 获取session中的变量值,以下用法是相同的:</span> $language <span class="token">=</span> $session<span class="token">-</span><span class="token">></span><span class="token1">get</span><span class="token2">(</span><span class="token4">'language'</span><span class="token2">)</span><span class="token2">;</span> $language <span class="token">=</span> $session<span class="token2">[</span><span class="token4">'language'</span><span class="token2">]</span><span class="token2">;</span> $language <span class="token">=</span> <span class="token1">isset</span><span class="token2">(</span>$_SESSION<span class="token2">[</span><span class="token4">'language'</span><span class="token2">]</span><span class="token2">)</span> <span class="token">?</span> $_SESSION<span class="token2">[</span><span class="token4">'language'</span><span class="token2">]</span> <span class="token2">:</span> <span class="token5">null</span><span class="token2">;</span> <span class="token6">// 设置一个session变量,以下用法是相同的:</span> $session<span class="token">-</span><span class="token">></span><span class="token1">set</span><span class="token2">(</span><span class="token4">'language'</span><span class="token2">,</span> <span class="token4">'en-US'</span><span class="token2">)</span><span class="token2">;</span> $session<span class="token2">[</span><span class="token4">'language'</span><span class="token2">]</span> <span class="token">=</span> <span class="token4">'en-US'</span><span class="token2">;</span> $_SESSION<span class="token2">[</span><span class="token4">'language'</span><span class="token2">]</span> <span class="token">=</span> <span class="token4">'en-US'</span><span class="token2">;</span> <span class="token6">// 删除一个session变量,以下用法是相同的:</span> $session<span class="token">-</span><span class="token">></span><span class="token1">remove</span><span class="token2">(</span><span class="token4">'language'</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">unset</span><span class="token2">(</span>$session<span class="token2">[</span><span class="token4">'language'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">unset</span><span class="token2">(</span>$_SESSION<span class="token2">[</span><span class="token4">'language'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">// 检查session变量是否已存在,以下用法是相同的:</span> <span class="token5">if</span> <span class="token2">(</span>$session<span class="token">-</span><span class="token">></span><span class="token1">has</span><span class="token2">(</span><span class="token4">'language'</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">.</span><span class="token2">.</span><span class="token2">.</span> <span class="token5">if</span> <span class="token2">(</span><span class="token1">isset</span><span class="token2">(</span>$session<span class="token2">[</span><span class="token4">'language'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">.</span><span class="token2">.</span><span class="token2">.</span> <span class="token5">if</span> <span class="token2">(</span><span class="token1">isset</span><span class="token2">(</span>$_SESSION<span class="token2">[</span><span class="token4">'language'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">.</span><span class="token2">.</span><span class="token2">.</span> <span class="token6">// 遍历所有session变量,以下用法是相同的:</span> foreach <span class="token2">(</span>$session as $name <span class="token">=</span><span class="token">></span> $value<span class="token2">)</span> <span class="token2">.</span><span class="token2">.</span><span class="token2">.</span> foreach <span class="token2">(</span>$_SESSION as $name <span class="token">=</span><span class="token">></span> $value<span class="token2">)</span> <span class="token2">.</span><span class="token2">.</span><span class="token2">.</span> ``` ``` cookie ``` <pre class="calibre14">``` <span class="token6">// 从 "request" 组件中获取 cookie 集合(yii\web\CookieCollection)</span> $cookies <span class="token">=</span> Yii<span class="token2">:</span><span class="token2">:</span>$app<span class="token">-</span><span class="token">></span>request<span class="token">-</span><span class="token">></span>cookies<span class="token2">;</span> <span class="token6">// 获取名为 "language" cookie 的值,如果不存在,返回默认值 "en"</span> $language <span class="token">=</span> $cookies<span class="token">-</span><span class="token">></span><span class="token1">getValue</span><span class="token2">(</span><span class="token4">'language'</span><span class="token2">,</span> <span class="token4">'en'</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">// 另一种方式获取名为 "language" cookie 的值</span> <span class="token5">if</span> <span class="token2">(</span><span class="token2">(</span>$cookie <span class="token">=</span> $cookies<span class="token">-</span><span class="token">></span><span class="token1">get</span><span class="token2">(</span><span class="token4">'language'</span><span class="token2">)</span><span class="token2">)</span> <span class="token">!==</span> <span class="token5">null</span><span class="token2">)</span> <span class="token2">{</span> $language <span class="token">=</span> $cookie<span class="token">-</span><span class="token">></span>value<span class="token2">;</span> <span class="token2">}</span> <span class="token6">// 可将 $cookies 当作数组使用</span> <span class="token5">if</span> <span class="token2">(</span><span class="token1">isset</span><span class="token2">(</span>$cookies<span class="token2">[</span><span class="token4">'language'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> $language <span class="token">=</span> $cookies<span class="token2">[</span><span class="token4">'language'</span><span class="token2">]</span><span class="token">-</span><span class="token">></span>value<span class="token2">;</span> <span class="token2">}</span> <span class="token6">// 判断是否存在名为"language" 的 cookie</span> <span class="token5">if</span> <span class="token2">(</span>$cookies<span class="token">-</span><span class="token">></span><span class="token1">has</span><span class="token2">(</span><span class="token4">'language'</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">.</span><span class="token2">.</span><span class="token2">.</span> <span class="token5">if</span> <span class="token2">(</span><span class="token1">isset</span><span class="token2">(</span>$cookies<span class="token2">[</span><span class="token4">'language'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">.</span><span class="token2">.</span><span class="token2">.</span> <span class="token6">// 从 "response" 组件中获取 cookie 集合(yii\web\CookieCollection)</span> $cookies <span class="token">=</span> Yii<span class="token2">:</span><span class="token2">:</span>$app<span class="token">-</span><span class="token">></span>response<span class="token">-</span><span class="token">></span>cookies<span class="token2">;</span> <span class="token6">// 在要发送的响应中添加一个新的 cookie</span> $cookies<span class="token">-</span><span class="token">></span><span class="token1">add</span><span class="token2">(</span><span class="token5">new</span> <span class="token1"><span class="token2">\</span>yii<span class="token2">\</span>web<span class="token2">\</span>Cookie</span><span class="token2">(</span><span class="token2">[</span> <span class="token4">'name'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'language'</span><span class="token2">,</span> <span class="token4">'value'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'zh-CN'</span><span class="token2">,</span> <span class="token2">]</span><span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">// 删除一个 cookie</span> $cookies<span class="token">-</span><span class="token">></span><span class="token1">remove</span><span class="token2">(</span><span class="token4">'language'</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">// 等同于以下删除代码</span> <span class="token1">unset</span><span class="token2">(</span>$cookies<span class="token2">[</span><span class="token4">'language'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> ``` ``` 日志 ``` <pre class="calibre16">``` Yii<span class="token2">:</span><span class="token2">:</span><span class="token1">error</span><span class="token2">(</span><span class="token4">'日志文件'</span><span class="token2">)</span><span class="token2">;</span> ``` ```