2012-04-03 157 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]); 
    } 
}