2014-04-12 23 views
0

我想重定向slu that是不正確的,但我得到錯誤的URL沒有檢測到。如何重新定向如果slug在codeigniter中不正確?

這是我的控制器:

public function index($id = null, $slug = ''){ 
// Fetch the article 
     //$this->db->where('pubdate <=', date('Y-m-d')); 
     $this->data['article'] = $this->mberita->get_by_id($id,$slug); 
     // Return 404 if not found 
     count($this->data['article']) || show_404(uri_string()); 

     // Redirect if slug was incorrect 
     $requested_slug = $this->uri->segment(3); 
     $set_slug = $this->data['article']->slug; 
     if ($requested_slug != $set_slug) { 
      redirect('article/' . $this->data['article']->id . '/' . url_title($set_slug), 'location', '301'); 


     // Load view 
     $this->data['contents'] = 'article'; 
     $this->load->view('template/wrapper/mahasiswa/wrapper_article', $this->data); 

    } 

這是我的看法:

<div class="post-buttons"> 
     <a href="<?php echo 'article/' . intval($dt->id) . '/' . url_title($dt->slug) ; ?>" class="ui black label"> 
      Read More 
     </a> 
</div> 

我試試這個網址:未檢測

tkd/index.php/article/77/kejurnas-mahasiswa 

但URL。你能幫我做些什麼嗎?

謝謝。

+0

顯示.htaccess文件。 –

+0

.htaccess文件是「全部拒絕」如何? – user3459150

+0

@RPM OP尚未使用任何'.htacces'文件,OP在URL中仍有「index.php」。上面''.htaccess'的內容'全部拒絕'位於與OP問題無關的應用程序文件夾中。 – Kyslik

回答

0
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?/$1 [L]  
+0

此答案被標記爲低質量。請更新相關信息以使其更具價值。 – paqogomez

0

我長看了你的帖子,整天對你的魔法蛞蝓,對他們沒有什麼,但嘿,爲什麼你想有任何的蛞蝓在所有當你在你的網址有ID?

首先CodeIgniters index()函數沒有采取任何參數,你需要創建僞索引頁面+編輯routes.php(我會在後面的答案中解釋)或_remap()你的控制器。如果你想用_remap()我簡要解釋一下here

而是採用指數「參數」使用URI段(因爲你是通過$this->uri->segment(n)做的話)


讓我們回到地步,你正在使用的蛞蝓,我明白,你只是想得到如下:site.ufo/article/{id}/whatever-text-can-be-here-id-is-important

爲了達到此問題,請在您的routes.php

$route['article/(:num)/(:any)'] = "article/show_article/$1/$2"; //where article is controller and show_article is "behind the scene" method that takes over your index(); not a single user will know about it 
這條路線

現在讓我們對您的類命名文章進行一些更改。

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Article extends CI_Controller { 
//I can spot that you are extending your controller NOT by CI_Controller because of usage $this->data[] 

    public function __construct() { 
     //constructor 
     parent::__construct(); 
     $this->load->model('article_model', 'article'); //load model article_model and create alias "article" 

    } 

    public function index() { 
     //let this be empty or redirect if no ID is sent to the controller 
     redirect(); //redirect to default_controller 
    } 

    public function show_article($id = FALSE, $slug = FALSE) { 
     if ($id === FALSE) redirect(); // no id? redirect to default_controller 
     if ($slug === FALSE) { 
      //slug is not sent to the article 
      //you should do nothing if this happens but you may require reload page with correct slug 
       //get slug by id 
       //redirect(/article/$id/slug_by_id) 
     } 

     // Fetch the article 
     //$this->db->where('pubdate <=', date('Y-m-d')); 
     if ($this->data['article'] = $this->article->get($id)) { 
      //all good we can show result 
      $this->data['contents'] = 'article'; 
      $this->load->view('template/wrapper/mahasiswa/wrapper_article', $this->data); 

     } else { 
      //get() method returns FALSE 
      redirect(); //id is not valid or there is no such record with id = $id 
     } 

    } 

} 

/* End of file article.php */ 
/* Location: ./application/controllers/article.php */ 

最後,創建文件(模型)application/models/article_model.php

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Article extends CI_Model { 

    public function __construct() { 
     parent::__construct(); 
     //Do your magic here 
    } 

    public function get($id = FALSE) { 
     if ($id === FALSE) return FALSE; 

     $q = $this->db->get_where('article', array('id' => $id)); // table = 'article' 

     return ($q->num_rows() > 0) ? $q->result() : FALSE; 

    } 


} 

/* End of file article_model.php */ 
/* Location: ./application/models/article_model.php */ 
相關問題