2012-04-03 156 views
0

我有一個頁面,我從數據庫中拉出一個特定的行,其名稱等於我放入的URL,但我希望它對SEO友好。Preg替換問題

所以說我在數據庫中有「David's Print」。

我希望能夠通過投入「戴維斯打印」,在URL

我使用笨作爲我的框架,從數據庫中拉出。

如果我需要更好地解釋,讓我知道

目前我使用

public function item($name) 
{ 
    $this->data['item'] = $this->db->get_where('items', array('name' => str_replace("-", " ", $name)))->result_array(); 
    $this->layouts->view('databases/items/single', $this->data); 
} 

當然,這僅適用於空間

+0

這是使用像lucene這樣的搜索引擎更容易完成的事情,而不是db – 2012-04-03 12:36:30

+0

codeigniter的get_where部分,我會如何放置數組('name'=> $ name)($ name field thing ) – unlucky4ever 2012-04-03 12:37:02

+2

您必須將「slug」存儲在數據庫中。將'davids-print'''去掉''David's Print「'很難。編輯:是「不插電」是一個字。 – vstm 2012-04-03 12:40:45

回答

3

這是最好的存儲搜索引擎友好的網站ID或slugs,因爲他們是所謂的,旁邊的項目的實際標題。由於某些角色丟失了,難以將slu convert轉換回原始標題。

首先將slug字段添加到您的items表中,並將其設置爲唯一,以便沒有歧義(因此每個項目都有自己的slug)。

每當一個項目是插入或更新,然後產生的廢料,例如:

$this->title = $_POST['title']; 
$this->slug = url_title($this->title, 'dash', TRUE); 

$this->db->insert('items', $this); 

如果DB拋出,因爲「獨一無二」的約束,你可以嘗試記錄多次插入的一個例外,只需在slu add中添加一個數字即可。

然後你就可以輕鬆地搜索使用蛞蝓的項目($name包含URL部分像davids-print):

$this->data['item'] = $this->db-> 
    get_where('items', array('slug' => $name))->result_array(); 

感謝Madmartigan的建議,使用url_title其自帶的CI。

+1

對於「添加一個數字到slug」位,CI的'increment_string()'函數是可用的(儘管我希望看到一個錯誤:「slug被採取,選擇另一個」)。此外,我會建議截斷塞爾90個字左右(或類似地,顯示一個錯誤,它太長)。 – 2012-04-03 13:46:25

1

,但我有一個擴展的路由器類,將想要你想要的。

它將解析任何具有' - '的控制器方法和變量並將其替換爲'_'。

你可以這樣做;

public function item($name) 
{ 
    $this->data['item'] = $this->db->get_where('items', array('name' => str_replace("_", " ", $name)))->result_array(); 
    $this->layouts->view('databases/items/single', $this->data); 
} 

類(MY_Router.php)需要被放在應用/核心(介意類擴展)。

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
class MY_Router extends CI_Router { 

    function set_class($class) { 
     $this->class = str_replace('-', '_', $class); 
    } 

    function set_method($method) { 
     $this->method = str_replace('-', '_', $method); 
    } 

    function _validate_request($segments) { 
     // Does the requested controller exist in the root folder? 
     if (file_exists(APPPATH.'controllers/'.str_replace('-', '_', $segments[0]).EXT)) { 
      return $segments; 
     } 
     // Is the controller in a sub-folder? 
     if (is_dir(APPPATH.'controllers/'.$segments[0])) {  
      // Set the directory and remove it from the segment array 
      $this->set_directory($segments[0]); 
      $segments = array_slice($segments, 1); 

      if (count($segments) > 0) { 
       // Does the requested controller exist in the sub-folder? 
       if (! file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $segments[0]).EXT)) { 
        show_404($this->fetch_directory().$segments[0]); 
       } 
      } else { 
       $this->set_class($this->default_controller); 
       $this->set_method('index'); 

       // Does the default controller exist in the sub-folder? 
       if (! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT)) { 
        $this->directory = ''; 
        return array(); 
       } 

      } 

      return $segments; 
     } 

     // Can't find the requested controller... 
     show_404($segments[0]); 
    } 
}