0

我正在尝试为我的投资组合中的博客创建 URL 友好的链接。所以我想获得类似 site/journal/post/{title} 的链接

显然 Journal 是我的控制器,但假设我的标题是“mysite.com 上线!” 我想要一个有效的网址,例如 site/journal/post/mysitecom-goes-live ,其中所有不允许的字符都被删除。

我将如何转换“mysite.com 上线!” 根据 $config['permitted_uri_chars'] 中的字符到 CodeIgniter 中的 'site/journal/post/mysitecom-goes-live'

4

1 回答 1

3

使用 url 助手

$this->load->helper('url');

$blog_slug = url_title('Mysite.com Goes live!');

echo $blog_slug //mysitecom-site-goes-live 
// might differ slightly, but it'll do what you want.

生成对 url 友好的链接。

将此值存储在博客表 (url_title/url_slug) 中的字段中。

做一个功能:

class Journal extends controller
{
   //make your index/constructor etc

   function view($post)
   {
     $this->blog_model->get_post($post);
     // etc - your model returns the correct post,
     // then process that data and pass it to your view
   }
}

您的 blog_model 有一个使用 CI 的 get_post 方法

$this->db->where('url_title', $post);

希望这是有道理的。

然后当您访问该页面时:

site.com/journal/view/mysite-goes-live

该函数将获取“mysite-goes-live”并将其传递给 view() 函数,该函数依次在数据库中查找相应的博客条目。

于 2010-10-14T16:47:12.080 回答