2013-04-21 79 views
0

我的問題是如何在codeigniter url中追加國名?codeigniter url中的國家名

比方說我的網站是http://mywebsite.com,我打開加拿大的網站,所以我的網址應該是http://mywebsite.com/canada。 含義我只想在URL中追加國名,除此之外沒有任何變化。我已經讀過關於codeigniter的路由,我已經使用了google,但都是徒勞的。

如果任何人有線索如何做到這一點,請讓我知道。

+0

這樣做的目的是什麼?除非您爲不同的國家/地區提供單獨的內容?你應該多解釋一下。 – 2013-04-22 00:20:35

回答

0

我想你會用IP查找來根據請求IP地址猜測國家($ _SERVER ['REMOTE_ADDR'])。將IP地址映射到一個位置是pretty well documented.一旦你擁有了國家,就可以重定向到正確的控制器方法。

你在找什麼?

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); 

的index.php

<?php 

require('set_country_uri.php'); 

//ci code ... 

因此,在你的代碼CI使用COUNTRY常量知道至極使用。在不考慮uri國家的情況下,讓你的代碼像路由一樣。