• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ericlbarnes/CodeIgniter-Slug-Library: Generate friendly uri strings

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

ericlbarnes/CodeIgniter-Slug-Library

开源软件地址(OpenSource Url):

https://github.com/ericlbarnes/CodeIgniter-Slug-Library

开源编程语言(OpenSource Language):

PHP 100.0%

开源软件介绍(OpenSource Introduction):

CodeIgniter Slug Library

This library is designed to help you generate friendly uri strings for your content stored in the database.

For example if you have a blog post table then you would want uri strings such as: mysite.com/post/my-post-title

The problem with this is each post needs a unique uri string and this library is designed to handle that for you.

So if you add another with the same uri or title it would convert it to: mysite.com/post/my-post-title-2

Requirements

  • CodeIgniter
  • Some form of database supported by active record

Usage

Here is an example setup:

Please note that these fields map to your database table fields.

$config = array(
	'field' => 'uri',
	'title' => 'title',
	'table' => 'mytable',
	'id' => 'id',
);
$this->load->library('slug', $config);

Adding and Editing Records:

When creating a uri for adding to the database you will use something like this:

$data = array(
	'title' => 'My Test',
);
$data['uri'] = $this->slug->create_uri($data);
$this->db->insert('mytable, $data);

Then for editing: (Notice the create_uri uses the second param to compare against other fields).

$id = 1;
$data = array(
	'title' => 'My Test',
);
$data['uri'] = $this->slug->create_uri($data, $id);
$this->db->where('id', $id);
$this->db->update('mytable', $data);

Methods

__construct($config = array())

Setup the library with your config options.

$config = array(
	'table' => 'mytable',
	'id' => 'id',
	'field' => 'uri',
	'title' => 'title',
	'replacement' => 'dash' // Either dash or underscore
);
$this->load->library('slug', $config);

set_config($config = array())

Pass an array of config vars that will override setup

Paramaters

  • $config - (required) - Array of config options
$config = array(
	'table' => 'mytable',
	'id' => 'id',
	'field' => 'uri',
	'title' => 'title',
	'replacement' => 'dash' // Either dash or underscore
);
$this->slug->set_config($config);

create_uri($data = '', $id = '')

Creates the actual uri string and in the background validates against the table to ensure it is unique.

Paramaters

  • $data - (requied) Array of data
  • $id - (optional) Id of current record
$data = array(
	'title' => 'My Test',
);
$this->slug->create_uri($data)
$data = array(
	'title' => 'My Test',
);
$this->slug->create_uri($data, 1)

This returns a string of the new uri.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap