0

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

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

如果有人知道如何做到这一点,请告诉我。

4

2 回答 2

0

这就像 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);

索引.php

<?php

require('set_country_uri.php');

//ci code ...

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

于 2013-04-22T00:21:02.707 回答
0

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

那就是你要找的东西?

于 2013-04-22T00:02:26.627 回答