多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# URL生成 ## URL 生成 用于生成URL链接地址,系统提供的助手函数为`url`。 **函数说明** ``` <pre class="calibre16">``` <span class="token5">/** * Url生成 * @param string $url 路由地址 * @param array $vars 数组变量 * @param bool|string $suffix 生成的URL后缀 ,例如html,默认是html * @param bool|string $domain 域名 * @return UrlBuild */</span> <span class="token2">function</span> <span class="token6">url</span><span class="token1">(</span>string $url <span class="token">=</span> <span class="token4">''</span><span class="token1">,</span> array $vars <span class="token">=</span> <span class="token1">[</span><span class="token1">]</span><span class="token1">,</span> $suffix <span class="token">=</span> <span class="token3">true</span><span class="token1">,</span> $domain <span class="token">=</span> <span class="token3">false</span><span class="token1">)</span><span class="token1">:</span> UrlBuild ``` ``` 模板使用方式 ``` <pre class="calibre16">``` <span class="token1">{</span><span class="token1">:</span><span class="token6">url</span><span class="token1">(</span><span class="token4">'/Index/demo'</span><span class="token1">)</span><span class="token1">}</span> ``` ``` ## 入口文件说明 url 生成跟入口文件配置挂钩,这里为了兼容不支持伪静态的默认是显示入口文件的,如果你配置了伪静态是可以选择隐藏的,这样生成的url地址不会带入口文件。 ![](https://img.kancloud.cn/ee/29/ee29b530db8c768b4f7e9f1d62369812_652x395.png) ## 不使用路由方式生成 ### 1. 应用内访问 带有参数形式生成,参数1的格式为:`url('控制器/操作方法')` ``` <pre class="calibre16">``` <span class="token5">// url生成,默认是带有html后缀的,url方法返回的是对象可方便额外的操作,使用(string)转字符串</span> $url <span class="token">=</span> <span class="token1">(</span>string<span class="token1">)</span> <span class="token6">url</span><span class="token1">(</span><span class="token4">'/index/demo'</span><span class="token1">,</span><span class="token1">[</span><span class="token4">'aaa'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">dd</span><span class="token1">(</span>$url<span class="token1">)</span><span class="token1">;</span> <span class="token5">// 输出结果</span> ``` ``` 前台入口文件隐藏后效果,如果未配置隐藏入口文件,则前面会有`/index.php/`,以下示例默认是配置了入口文件隐藏。 ``` <pre class="calibre16">``` <span class="token">/</span>index<span class="token">/</span>demo<span class="token1">.</span>html<span class="token">?</span>aaa<span class="token">=</span><span class="token3">1</span> ``` ``` 生成带有域名的链接地址,第四个参数为`true`表示生成的链接地址加域名 ``` <pre class="calibre16">``` <span class="token5">// 自动生成域名、加后缀</span> $url <span class="token">=</span> <span class="token1">(</span>string<span class="token1">)</span><span class="token6">url</span><span class="token1">(</span><span class="token4">'/demo/index'</span><span class="token1">,</span><span class="token1">[</span><span class="token4">'aaa'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token1">]</span><span class="token1">,</span><span class="token4">'html'</span><span class="token1">,</span><span class="token3">true</span><span class="token1">)</span><span class="token1">;</span> <span class="token5">// 结果</span> http<span class="token1">:</span><span class="token">/</span><span class="token">/</span>xxx<span class="token1">.</span>cms<span class="token1">.</span>com<span class="token">/</span>demo<span class="token">/</span>index<span class="token1">.</span>html<span class="token">?</span>aaa<span class="token">=</span><span class="token3">1</span> ``` ``` 生成锚点示例,生成锚点注意前面不需要加`/`,如下 ``` <pre class="calibre16">``` <span class="token5">// 锚点跟子域名同时存在时需要锚点在前,域名在后</span> $url <span class="token">=</span> <span class="token1">(</span>string<span class="token1">)</span><span class="token6">url</span><span class="token1">(</span><span class="token4">'index/demo#ddd'</span><span class="token1">,</span><span class="token1">[</span><span class="token4">'aaa'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token1">,</span><span class="token4">'b'</span><span class="token">=</span><span class="token">></span><span class="token4">'sfsfs'</span><span class="token1">]</span><span class="token1">,</span><span class="token4">''</span><span class="token1">,</span><span class="token3">true</span><span class="token1">)</span><span class="token1">;</span> <span class="token5">// 结果</span> http<span class="token1">:</span><span class="token">/</span><span class="token">/</span>xxx<span class="token1">.</span>cms<span class="token1">.</span>com<span class="token1">:</span><span class="token3">8080</span><span class="token">/</span>index<span class="token">/</span>demo<span class="token">?</span>aaa<span class="token">=</span><span class="token3">1</span><span class="token">&</span>b<span class="token">=</span>sfsfs#ddd ``` ``` ### 2. 访问其他应用,域名绑定 域名绑定配置在`config/app.php`中,目前有以下配置,这里配置好后记得在域名提供商做好解析。 ``` <pre class="calibre16">``` <span class="token5">// 域名绑定</span> <span class="token4">'domain_bind'</span> <span class="token">=</span><span class="token">></span> <span class="token1">[</span> <span class="token4">'api'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'api'</span><span class="token1">,</span> <span class="token4">'*'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'index'</span><span class="token1">,</span> <span class="token1">]</span><span class="token1">,</span> ``` ``` 示例:生成锚点并绑定到`api`域名,通过生成后的链接即可访问到`api`应用。提示:如果入口文件未配置隐藏,那么入口文件还是存在的。 ``` <pre class="calibre16">``` <span class="token5">// 生成锚点,并绑定域名</span> $url <span class="token">=</span> <span class="token1">(</span>string<span class="token1">)</span><span class="token6">url</span><span class="token1">(</span><span class="token4">'index/demo#ddd@api'</span><span class="token1">,</span><span class="token1">[</span><span class="token4">'aaa'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token1">,</span><span class="token4">'b'</span><span class="token">=</span><span class="token">></span><span class="token4">'sfsfs'</span><span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token5">// 隐藏入口文件结果</span> http<span class="token1">:</span><span class="token">/</span><span class="token">/</span>api<span class="token1">.</span>cms<span class="token1">.</span>com<span class="token">/</span>index<span class="token">/</span>demo<span class="token1">.</span>html<span class="token">?</span>aaa<span class="token">=</span><span class="token3">1</span><span class="token">&</span>b<span class="token">=</span>sfsfs#ddd <span class="token5">// 未隐藏结果</span> http<span class="token1">:</span><span class="token">/</span><span class="token">/</span>api<span class="token1">.</span>cms<span class="token1">.</span>com<span class="token">/</span>index<span class="token1">.</span>php<span class="token">/</span>index<span class="token">/</span>demo<span class="token1">.</span>html<span class="token">?</span>aaa<span class="token">=</span><span class="token3">1</span><span class="token">&</span>b<span class="token">=</span>sfsfs#ddd ``` ``` 示例2:也可使用url方法的参数4来实现,如下 ``` <pre class="calibre16">``` <span class="token5">// 指定子域名为api</span> $url <span class="token">=</span> <span class="token1">(</span>string<span class="token1">)</span><span class="token6">url</span><span class="token1">(</span><span class="token4">'/index/demo'</span><span class="token1">,</span><span class="token1">[</span><span class="token4">'aaa'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token1">]</span><span class="token1">,</span><span class="token4">'html'</span><span class="token1">,</span><span class="token4">'api'</span><span class="token1">)</span><span class="token1">;</span> <span class="token5">// 也支持完整域名,如果不是80端口需要域名+端口</span> $url <span class="token">=</span> <span class="token1">(</span>string<span class="token1">)</span><span class="token6">url</span><span class="token1">(</span><span class="token4">'/index/demo'</span><span class="token1">,</span><span class="token1">[</span><span class="token4">'aaa'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token1">]</span><span class="token1">,</span><span class="token4">'html'</span><span class="token1">,</span><span class="token4">'local.cms.com:8080'</span><span class="token1">)</span><span class="token1">;</span> ``` ``` ### 3. 访问其他应用,入口文件 如果不通过域名解析按入口文件来访问,需要生成其他应用链接时可以通过以下写法实现 ``` <pre class="calibre16">``` $url <span class="token">=</span> <span class="token1">(</span>string<span class="token1">)</span><span class="token6">url</span><span class="token1">(</span><span class="token4">'api:/index/demo'</span><span class="token1">,</span><span class="token1">[</span><span class="token4">'aaa'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token1">,</span><span class="token4">'b'</span><span class="token">=</span><span class="token">></span><span class="token4">'sfsfs'</span><span class="token1">]</span><span class="token1">,</span><span class="token4">''</span><span class="token1">,</span><span class="token3">true</span><span class="token1">)</span><span class="token1">;</span> ``` ``` 生成结果 ``` <pre class="calibre16">``` http<span class="token1">:</span><span class="token">/</span><span class="token">/</span>local<span class="token1">.</span>cms<span class="token1">.</span>com<span class="token">/</span>api<span class="token1">.</span>php<span class="token">/</span>index<span class="token">/</span>demo<span class="token">?</span>aaa<span class="token">=</span><span class="token3">1</span><span class="token">&</span>b<span class="token">=</span>sfsfs ``` ``` ## 使用路由方式生成 了解这里前先了解路由定义,假设`index`应用配置了以下路由 ``` <pre class="calibre16">``` Route<span class="token1">:</span><span class="token1">:</span><span class="token6">rule</span><span class="token1">(</span><span class="token4">'/demo'</span><span class="token1">,</span><span class="token4">'/index/demo'</span><span class="token1">)</span><span class="token1">;</span> ``` ``` 根据路由生成地址写法如下,参数1的地址需要跟上面的参数2保持一致。 ``` <pre class="calibre16">``` $url <span class="token">=</span> <span class="token1">(</span>string<span class="token1">)</span><span class="token6">url</span><span class="token1">(</span><span class="token4">'/index/demo'</span><span class="token1">,</span><span class="token1">[</span><span class="token4">'aaa'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> ``` ``` 结果 ``` <pre class="calibre16">``` <span class="token">/</span>demo<span class="token1">.</span>html<span class="token">?</span>aaa<span class="token">=</span><span class="token3">1</span> ``` ``` 使用域名 ``` <pre class="calibre20">``` $url <span class="token">=</span> <span class="token1">(</span>string<span class="token1">)</span><span class="token6">url</span><span class="token1">(</span><span class="token4">'/index/demo'</span><span class="token1">,</span><span class="token1">[</span><span class="token4">'aaa'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token1">]</span><span class="token1">,</span><span class="token4">''</span><span class="token1">,</span><span class="token3">true</span><span class="token1">)</span><span class="token1">;</span> ``` ```