我在routes.php文件的解決方案:
$route['news'] = 'news_controller';
$route['gallery'] = 'gallery_controller';
$route['(:any)'] = 'sites/$1';
,並在我的網站電腦板:
function index($site_id = '') {
//sanitize $site_id.
$this->site = $this->sites_model->get_site($site_id);
//etc.
}
THX嚴
問題:
所以我用CodeIgniter寫了一個小CMS。管理員可以創建網站。當網段的段與DB中的網段相似時,網站會自動打開。例如mysite.com/sites/about會打電話給「關於」網站。這工作正常。
現在我的網址出現了問題。我希望這個網址
http://www.mysite.com/sites/about
變成這樣:
的問題是,我無法使用routes.php文件,併爲每個站點設置通配符。 (因爲它們是動態的,我不知道該網站的客戶將創建 - 我不想編輯他將創建的每個網站routes.php文件 - 這應該自動完成)
問題是我得到了其他修復控制器太像新聞,圖庫,或聯繫: mysite.com/news,mysite.com/gallery,......他們做工精細
所以這裏是我的地盤控制器:
class Sites extends Public_Controller {
public $site;
public $url_segment;
public function _remap($method)
{
$this->url_segment = $this->uri->segment(2);
$this->load->model('sites_model');
$this->site = $this->sites_model->get_site($this->url_segment);
if($this->site !== FALSE)
{
$this->show_site($this->site);
}
else
{
show_404($this->url_segment);
}
}
public function show_site($data)
{
$this->template->set('site', FALSE);
$this->template->set('site_title', $data['name']);
$this->template->set('content',$data['content']);
$this->template->load('public/template','sites/sites_view', $data);
}}
這是檢查數據庫的Site_model ...如果url_segment符合DB中的標題:
class Sites_model extends CI_Model {
public function get_site($site_url)
{
if($site_url != ""){
$this->db->where('name', $site_url);
$query = $this->db->get('sites', 1);
if($query->num_rows() > 0){
return $query->row_array();
}
else
{
return FALSE;
}
}else{
return FALSE;
}
} }
我想我需要的東西,誰檢查,如果存在控制器(URL的第一部分)時,不叫站點控制器,並檢查該網站是在數據庫中,當這是假的,然後撥打404
有什麼建議如何解決這個問題?
BTW:SRY,我的英語
問候GN
SRY我的英語(奧地利) 耶這就是真的,那麼他會叫其他的控制器壞了。但最後我不想要段「網站」。從mysite.com/sites/about到mysite /約 我喜歡隱藏段「網站」 – gn90at