【问题标题】:Country name in codeigniter urlcodeigniter url中的国家名称
【发布时间】:2013-04-14 18:35:56
【问题描述】:

我的问题是如何在 codeigniter url 中附加国家名称?

假设我的网站是http://mywebsite.com,我从加拿大打开网站,所以我的网址应该是http://mywebsite.com/canada。 这意味着我只想在 url 中附加国家名称,除此之外没有任何变化。我已经阅读了关于 codeigniter 的路线,我已经用谷歌搜索了它,但都是徒劳的。

如果有人知道如何执行此操作,请告诉我。

【问题讨论】:

  • 除非您为不同的国家/地区提供单独的内容,否则这样做的目的是什么?你应该多解释一下。

标签: codeigniter codeigniter-url


【解决方案1】:

我猜你会通过 IP 查找来根据请求 IP 地址 ($_SERVER['REMOTE_ADDR']) 猜测国家/地区。将IP地址映射到一个位置是pretty well documented.一旦你有了国家,重定向到正确的控制器方法。

这就是你要找的东西?

【讨论】:

    【解决方案2】:

    这就像 url 中的语言代码。

    使用我的解决方案,您可以将任何段放入您的 uri 并将其隐藏到 CI。

    http://site.com/page.html 将等于 http://site.com/canada/page.html,反之亦然。

    set_country_uri.php

    //App location
    $ci_directory = '/ci/';
    
    //countries
    $countries = array('canada','france');
    
    //default country
    $country = 'canada';
    
    foreach( $countries as $c )
    {
        if(strpos($_SERVER['REQUEST_URI'], $ci_directory.$c)===0)
        {
            //Store country founded
            $country = $c;
    
            //Delete country from URI, codeigniter will don't know is exists !
            $_SERVER['REQUEST_URI'] = substr_replace($_SERVER['REQUEST_URI'], '', strpos($_SERVER['REQUEST_URI'], '/'.$c)+1, strlen($c)+1);
    
            break;
        }
    }
    
    //Add the country in URI, for site_url() function
    $assign_to_config['index_page'] = $country."/";
    
    //store country founded, to be able access it in your app
    define('COUNTRY', $country);
    

    index.php

    <?php
    
    require('set_country_uri.php');
    
    //ci code ...
    

    所以在你的 CI 代码中使用 COUNTRY 常量来知道使用的是哪个。并使您的代码像路由一样,而不考虑 uri 中的国家/地区。

    【讨论】:

      猜你喜欢
      • 2016-06-11
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多