什么叫通用转发接口?

    例如地址为: /?=load 参数是表示加载页面功能,license 参数是调用 routes 目录下的 license.php 文件。

    如果没有找到匹配的文件或者没有参数,就默认加载 home()。例如:

    1. 主索引脚本。


    2. Rewrite(服务器重写技术)
    Apache

    1. RewriteEngine on

    2. RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f

    3. RewriteRule ^(.*)$ /?load=$1 [QSA,PT,L]


    Nginx

    1. if (!-e $request_filename) {
      rewrite ^/(.+)$ /?load=$1 last;
      }


    没有使用规则前:

    1. http://lang-php.com/?load=license



    使用后:

    1. http://lang-php.com/license



    官方网站已添加该规则,为保持发布版相同,请手动访问演示。


    什么叫通过 Javascript (JS) 来切换语言并隐藏参数?目的就是去掉后缀。

    使用模式 1 前:http://lang-php.com/?lang=zh-cn;

    使用模式 2 后:

    1. Javascript (JS) 脚本代码。

    1. <script type="text/javascript">
      /*

        • lang-php JavaScript Library v5.1
          *
        • http://lang-php.com
          *
        • Copyright 2016, FengYi, Inc.
          *
        • Released under the MIT license

        • *
        • Date: 2016-10-01
        • /
      • function refreshlang(value) {
        window.location.href = window.location.pathname + '?lang=' + value;
        }

      • function qlang(value) {
        var days = '30';

      • if (days) {
      •     var date = new Date();
      •     date.setTime(date.getTime()+(days*24*60*60*1000));
      •     var expires = &#34;; expires=&#34;+date.toGMTString();
      • else var expires = &#34;&#34;;
      • document.cookie = &#34;mark_lang&#34;+&#34;=&#34;+value.toLowerCase()+expires+&#34;; path=/&#34;;
      • window.location.href=&#34;../&#34;; 
      • function lang(value) {
        var days = '30';

      • if (days) {
      •     var date = new Date();
      •     date.setTime(date.getTime()+(days*24*60*60*1000));
      •     var expires = &#34;; expires=&#34;+date.toGMTString();
      • document.cookie = &#34;lang&#34;+&#34;=&#34;+value.toLowerCase()+expires+&#34;; path=/&#34;;
      • var rurl = window.location.toString();
      • //Remove anchor from url using the split
      • rurl = rurl.split(&#34;#&#34;)[0];
      • if(location.href.indexOf(&#34;?&#34;)==-1 || location.href.indexOf(name+&#39;=&#39;)==-1) {
      •     skip(rurl);
      • } else {
      •     skip(delQueStr(rurl, &#34;lang&#34;));
      • }
      • }

      • function skip(url) {
        // Internet Explorer 8 and lower fix
        if (navigator.userAgent.match(/MSIE\s(?!9.0)/)) {
        var referLink = document.createElement("a");
        referLink.href = url;
        document.body.appendChild(referLink);
        referLink.click();
        } else {
        // All other browsers
        window.location.replace(url);
        }
        }

      • function delQueStr(url, ref)
        {
        var str = "";

      • if (url.indexOf(&#39;?&#39;) != -1)
      • else
      •     return url;
      • var arr = &#34;&#34;;
      • var returnurl = &#34;&#34;;
      • if (str.indexOf(&#39;&amp;&#39;) != -1) {
      •     for (i in arr) {
      •         if (arr[i].split(&#39;=&#39;)[0] != ref) {
      •             returnurl = returnurl + arr[i].split(&#39;=&#39;)[0] + &#34;=&#34; + arr[i].split(&#39;=&#39;)[1] + &#34;&amp;&#34;;
      •         }
      •     }
      •     return url.substr(0, url.indexOf(&#39;?&#39;)) + &#34;?&#34; + returnurl.substr(0, returnurl.length - 1);
      • }
      • else {
      •     arr = str.split(&#39;=&#39;);
      •     if (arr[0] == ref)
      •         return url.substr(0, url.indexOf(&#39;?&#39;));
      •     else
      •         return url;
      • }
      • }
        </script>



      源码:latest.zip -> /assets/js/lang.js
      2. HTML 模板。

      1. <html>
        <headl>
        <script src="lang.js"></script>
        </headl>
        <body>
        <a href="?lang=en-us">切换</a>
        </body>
        </html>



      什么是跳转到指定的域名?部分示例都是通过读取并匹配目录下的文件,想实现跳转到指定的域名,请参考以下代码:

      添加代码到需要跳转的页面!

      1. <php
        / sample 1 /
        header("Location: http://us.lang-php.com&#34;);

      2. /* sample 2 */
      3. $Global_Domain = &#34;http://lang-php.com/&#34;;
      4. header(&#34;Location:&#34; . $Global_Domain . &#34;us&#34;);                


      源码:latest.zip -> /routes/Domain-Jump-Sample.php