Giter Club home page Giter Club logo

codeigniter-slug-library's 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.

codeigniter-slug-library's People

Contributors

ericlbarnes avatar od3n avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

codeigniter-slug-library's Issues

Unicode not supported

This Slug Library has not supported Unicode or Utf-8 text?
Ex: I have a text: "Đi chơi lúc nào cũng vui hơn đi học"
Then library convert to: "dji-choi-luc-nao-cung-vui-hon-dji-hoc" => with an excess of characters "j"
True is: "di-choi-luc-nao-cung-vui-hon-di-hoc"

Please update or let me know how to resolve it.

Allow user specify custom slug

I think users may specify a custom slug, sometimes (like on the projects I work on), users want to write their own slug and the Slug lib can just check and append de counter if it duplicates other records.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.