123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- <!DOCTYPE HTML>
- <html lang="" >
- <head>
- <meta charset="UTF-8">
- <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
- <title>Param · GitBook</title>
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- <meta name="description" content="">
- <meta name="generator" content="GitBook 3.2.3">
-
-
-
-
- <link rel="stylesheet" href="../gitbook/style.css">
-
-
-
- <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
-
-
-
- <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
-
-
-
- <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- <meta name="HandheldFriendly" content="true"/>
- <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
- <meta name="apple-mobile-web-app-capable" content="yes">
- <meta name="apple-mobile-web-app-status-bar-style" content="black">
- <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
- <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
-
- <link rel="next" href="tablehelper.html" />
-
-
- <link rel="prev" href="../qi-ta-bu-chong.html" />
-
- </head>
- <body>
-
- <div class="book">
- <div class="book-summary">
-
-
- <div id="book-search-input" role="search">
- <input type="text" placeholder="Type to search" />
- </div>
-
- <nav role="navigation">
-
- <ul class="summary">
-
-
-
-
-
-
-
- <li class="chapter " data-level="1.1" data-path="../">
-
- <a href="../">
-
-
- 简介
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.2" data-path="../chapter1.html">
-
- <a href="../chapter1.html">
-
-
- 基础用法
-
- </a>
-
-
- <ul class="articles">
-
-
- <li class="chapter " data-level="1.2.1" data-path="../chapter1/pei-zhi.html">
-
- <a href="../chapter1/pei-zhi.html">
-
-
- 配置
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.2.2" data-path="../chapter1/lu-you.html">
-
- <a href="../chapter1/lu-you.html">
-
-
- 路由
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.2.3" data-path="../chapter1/kong-zhi-qi.html">
-
- <a href="../chapter1/kong-zhi-qi.html">
-
-
- 控制器
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.2.4" data-path="../chapter1/mo-xing.html">
-
- <a href="../chapter1/mo-xing.html">
-
-
- 模型
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.2.5" data-path="../chapter1/shi-tu.html">
-
- <a href="../chapter1/shi-tu.html">
-
-
- 视图
-
- </a>
-
-
- </li>
-
- </ul>
-
- </li>
-
- <li class="chapter " data-level="1.3" data-path="../qi-ta-bu-chong.html">
-
- <a href="../qi-ta-bu-chong.html">
-
-
- 其他补充
-
- </a>
-
-
- <ul class="articles">
-
-
- <li class="chapter active" data-level="1.3.1" data-path="param.html">
-
- <a href="param.html">
-
-
- Param
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.3.2" data-path="tablehelper.html">
-
- <a href="tablehelper.html">
-
-
- TableHelper
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.3.3" data-path="ke-zhi-xing-jiao-ben.html">
-
- <a href="ke-zhi-xing-jiao-ben.html">
-
-
- 可执行脚本
-
- </a>
-
-
- </li>
-
- </ul>
-
- </li>
-
-
- <li class="divider"></li>
- <li>
- <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
- Published with GitBook
- </a>
- </li>
- </ul>
- </nav>
-
-
- </div>
- <div class="book-body">
-
- <div class="body-inner">
-
-
- <div class="book-header" role="navigation">
-
- <!-- Title -->
- <h1>
- <i class="fa fa-circle-o-notch fa-spin"></i>
- <a href=".." >Param</a>
- </h1>
- </div>
- <div class="page-wrapper" tabindex="-1" role="main">
- <div class="page-inner">
-
- <div id="book-search-results">
- <div class="search-noresults">
-
- <section class="normal markdown-section">
-
- <h3 id="param">Param</h3>
- <p><code>Param</code>为我们提供了丰富的参数校验规则。</p>
- <h6 id="基础配置">基础配置</h6>
- <p>需要包含<code>type(类型)</code>和<code>desc(描述)</code>,描述使接口参数含义更清晰,同时生成文档时昨晚参数说明。</p>
- <pre><code class="lang-php">$rules = [
- <span class="hljs-string">'id'</span> => [<span class="hljs-string">'type'</span> => <span class="hljs-string">'int'</span>, <span class="hljs-string">'desc'</span> => <span class="hljs-string">'用户id'</span>],
- ];
- <span class="hljs-comment">// 当type位于第一个参数时,可缩写为</span>
- $rules = [
- <span class="hljs-string">'id'</span> => [<span class="hljs-string">'int'</span>, <span class="hljs-string">'desc'</span> => <span class="hljs-string">'用户id'</span>],
- ];
- </code></pre>
- <h6 id="类型支持">类型支持</h6>
- <p><code>type</code>支持<code>int</code>,<code>string</code>,<code>array</code>,<code>float</code>,<code>ip</code>,<code>intArr</code>(array类型,元素为int类型),<code>strArr</code>(array类型,元素为string类型),<code>ipArr</code>(array类型,元素为ip类型);并且支持<code>或</code>组合。</p>
- <pre><code class="lang-php">$rules = [
- <span class="hljs-comment">// id可能为int或者string类型</span>
- <span class="hljs-string">'id'</span> => [<span class="hljs-string">'int/string'</span>, <span class="hljs-string">'desc'</span> => <span class="hljs-string">'业务id'</span>], <span class="hljs-comment">// id可能为int或者string</span>
- ];
- </code></pre>
- <h6 id="枚举支持">枚举支持</h6>
- <p>配置<code>enum</code>key支持枚举值</p>
- <pre><code class="lang-php">$rules = [
- <span class="hljs-string">'value'</span> => [<span class="hljs-string">'int'</span>, <span class="hljs-string">'enum'</span> => [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], <span class="hljs-string">'desc'</span> => <span class="hljs-string">'更新值'</span>],
- <span class="hljs-string">'field'</span> => [<span class="hljs-string">'string'</span>, <span class="hljs-string">'enum'</span> => [<span class="hljs-string">'field1'</span>, <span class="hljs-string">'field2'</span>], <span class="hljs-string">'desc'</span> => <span class="hljs-string">'更新字段'</span>],
- ];
- </code></pre>
- <h6 id="区间支持">区间支持</h6>
- <p><code>int</code>,<code>float</code>数值类型可以配置<code>range</code>支持区间限制</p>
- <pre><code class="lang-php">$rules = [
- <span class="hljs-comment">// 全闭合</span>
- <span class="hljs-string">'num'</span> => [<span class="hljs-string">'int'</span>, <span class="hljs-string">'range'</span> => <span class="hljs-string">'[1,10]'</span>, <span class="hljs-string">'desc'</span> => <span class="hljs-string">'号码'</span>],
- <span class="hljs-comment">// 半闭合</span>
- <span class="hljs-string">'weight'</span> => [<span class="hljs-string">'float'</span>, <span class="hljs-string">'range'</span> => <span class="hljs-string">'(0,200.1]'</span>, <span class="hljs-string">'desc'</span> => <span class="hljs-string">'体重'</span>],
- <span class="hljs-comment">// 不闭合</span>
- <span class="hljs-string">'height'</span> => [<span class="hljs-string">'float'</span>, <span class="hljs-string">'range'</span> => <span class="hljs-string">'(50.5,250.1)'</span>, <span class="hljs-string">'desc'</span> => <span class="hljs-string">'身高'</span>],
- ];
- </code></pre>
- <h6 id="长度支持">长度支持</h6>
- <p><code>string</code>类型配置<code>len</code>支持长度限制,使用方法和<code>range</code>类似</p>
- <pre><code class="lang-php">$rules = [
- <span class="hljs-string">'nick'</span> => [<span class="hljs-string">'string'</span>, <span class="hljs-string">'len'</span> => <span class="hljs-string">'[4,11)'</span>, <span class="hljs-string">'desc'</span> => <span class="hljs-string">'昵称'</span>],
- ];
- </code></pre>
- <p>正则支持</p>
- <p><code>string</code>类型配置<code>reg</code>支持正则校验。</p>
- <pre><code class="lang-php">$rules = [
- <span class="hljs-string">'username'</span> => [<span class="hljs-string">'string'</span>, <span class="hljs-string">'reg'</span> => <span class="hljs-string">'[^0-9A-Za-z]'</span>, <span class="hljs-string">'desc'</span> => <span class="hljs-string">'用户名'</span>],
- ];
- </code></pre>
- <h6 id="emptyablenullable开关">emptyable/nullable开关</h6>
- <p><code>emptyable</code>设置为<code>true</code>的时候,参数允许空值(<code>string</code>类型的参数<code>&nick=</code>);<code>nullable</code>设置为<code>true</code>的时候,参数可以缺省,这时可以配合<code>default</code>设置默认值。</p>
- <pre><code class="lang-php">$rules = [
- <span class="hljs-string">'nick'</span> => [<span class="hljs-string">'string'</span>, <span class="hljs-string">'desc'</span> => <span class="hljs-string">'昵称'</span>],
- ];
- <span class="hljs-comment">// input: &nick=</span>
- <span class="hljs-comment">// outpuy: param error</span>
- $rules = [
- <span class="hljs-string">'nick'</span> => [<span class="hljs-string">'string'</span>, <span class="hljs-string">'emptyable'</span> => <span class="hljs-keyword">true</span>, <span class="hljs-string">'desc'</span> => <span class="hljs-string">'昵称'</span>],
- ];
- <span class="hljs-comment">// input: &nick=</span>
- <span class="hljs-comment">// outpuy: ['nick' => '']</span>
- $rules = [
- <span class="hljs-string">'nick'</span> => [<span class="hljs-string">'string'</span>, <span class="hljs-string">'nullable'</span> => <span class="hljs-keyword">true</span>, <span class="hljs-string">'default'</span> => <span class="hljs-string">'solu'</span>, <span class="hljs-string">'desc'</span> => <span class="hljs-string">'昵称'</span>],
- ];
- <span class="hljs-comment">// input: &nick= or 不传参</span>
- <span class="hljs-comment">// outpuy: ['nick' => 'solu']</span>
- </code></pre>
- <h6 id="参数名通配符支持">参数名通配符支持</h6>
- <p>同一类型不确定参数个数的时候,可以使用参数名的通配符进行配置</p>
- <pre><code class="lang-php">$rules = [
- <span class="hljs-string">'num_*'</span> => [<span class="hljs-string">'int'</span>, <span class="hljs-string">'desc'</span> => <span class="hljs-string">'号码'</span>],
- ];
- <span class="hljs-comment">// input: &num_1=1&num_2=2&num_5=5 ...</span>
- <span class="hljs-comment">// output: ['num_1' => 1, 'num_2' => 2, 'num_3' => 3]</span>
- </code></pre>
-
- </section>
-
- </div>
- <div class="search-results">
- <div class="has-results">
-
- <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
- <ul class="search-results-list"></ul>
-
- </div>
- <div class="no-results">
-
- <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
-
- </div>
- </div>
- </div>
- </div>
- </div>
-
- </div>
-
-
- <a href="../qi-ta-bu-chong.html" class="navigation navigation-prev " aria-label="Previous page: 其他补充">
- <i class="fa fa-angle-left"></i>
- </a>
-
-
- <a href="tablehelper.html" class="navigation navigation-next " aria-label="Next page: TableHelper">
- <i class="fa fa-angle-right"></i>
- </a>
-
-
-
- </div>
- <script>
- var gitbook = gitbook || [];
- gitbook.push(function() {
- gitbook.page.hasChanged({"page":{"title":"Param","level":"1.3.1","depth":2,"next":{"title":"TableHelper","level":"1.3.2","depth":2,"path":"qi-ta-bu-chong/tablehelper.md","ref":"qi-ta-bu-chong/tablehelper.md","articles":[]},"previous":{"title":"其他补充","level":"1.3","depth":1,"path":"qi-ta-bu-chong.md","ref":"qi-ta-bu-chong.md","articles":[{"title":"Param","level":"1.3.1","depth":2,"path":"qi-ta-bu-chong/param.md","ref":"qi-ta-bu-chong/param.md","articles":[]},{"title":"TableHelper","level":"1.3.2","depth":2,"path":"qi-ta-bu-chong/tablehelper.md","ref":"qi-ta-bu-chong/tablehelper.md","articles":[]},{"title":"可执行脚本","level":"1.3.3","depth":2,"path":"qi-ta-bu-chong/ke-zhi-xing-jiao-ben.md","ref":"qi-ta-bu-chong/ke-zhi-xing-jiao-ben.md","articles":[]}]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"qi-ta-bu-chong/param.md","mtime":"2018-09-13T03:12:45.788Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-09-13T03:13:15.790Z"},"basePath":"..","book":{"language":""}});
- });
- </script>
- </div>
-
- <script src="../gitbook/gitbook.js"></script>
- <script src="../gitbook/theme.js"></script>
-
-
- <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
-
-
-
- <script src="../gitbook/gitbook-plugin-search/search.js"></script>
-
-
-
- <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
-
-
-
- <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
-
-
-
- <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
-
-
-
- <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
-
-
- </body>
- </html>
|