GithubHelp home page GithubHelp logo

laravel-bundle-sluggable's Introduction

Sluggable

Warning! This package is not maintained anymore as I've moved to Laravel 4 a long time ago.

Easy automatic slug generation for your Eloquent models.

Installing the Bundle

Install the bundle using Artisan:

php artisan bundle::install sluggable

Update your application/bundles.php file with:

'sluggable' => array( 'auto' => true ),

Updating your Models

Define a public static property $sluggable with the definitions (see [#Configuration] below for details):

class Post extends Eloquent
{

	public static $sluggable = array(
		'build_from' => 'title',
		'save_to'    => 'slug',
	);

}

That's it ... your model is now "sluggable"!

Using the Class

Saving a model is easy:

$post = new Post(array(
	'title'    => 'My Awesome Blog Post'
));

$post->save();

And so is retrieving the slug:

echo $post->slug;

Configuration

Configuration was designed to be as flexible as possible. You can set up defaults for all of your Eloquent models, and then override those settings for individual models.

By default, global configuration can be set in the application/config/sluggable.php file. If a configuration isn't set, then the bundle defaults from bundles/sluggable/config/sluggable.php are used. Here is an example configuration, with all the settings shown:

return array(
	'build_from' => null,
	'save_to'    => 'slug',
	'style'      => 'slug',
	'separator'  => '-',
	'unique'     => true,
	'on_update'  => false,
);

build_from is the field or array of fields from which to build the slug. Each $model->field is contactenated (with space separation) to build the sluggable string. This can be model attribues (i.e. fields in the database) or custom getters. So, for example, this works:

class Person extends Eloquent {

	public static $sluggable = array(
		'build_from' => 'fullname'
	);

	public function get_fullname() {
		return $this->firstname . ' ' . $this->lastname;
	}

}

If build_from is empty, false or null, then the value of $model->__toString() is used.

save_to is the field in your model where the slug is stored. By default, this is "slug". You need to create this column in your table when defining your schema:

Schema::create('posts', function($table)
{
	$table->increments('id');
	$table->string('title');
	$table->string('body');
	$table->string('slug');
	$table->timestamps();
});

style defines the method used to turn the sluggable string into a slug. Right now (version 1.0) the only option is "slug" which uses Laravel's Str::slug() method.

separator defines the separator used when building a slug. Default is a hyphen.

unique is a boolean defining whether slugs should be unique among all models of the given type. For example, if you have two blog posts and both are called "My Blog Post", then they will both sluggify to "my-blog-post" (when using Sluggable's default settings). This could be a problem, e.g. if you use the slug in URLs.

By turning unique on, then the second Post model will sluggify to "my-blog-post-1". If there is a third post with the same title, it will sluggify to "my-blog-post-2" and so on. Each subsequent model will get an incremental value appended to the end of the slug, ensuring uniqueness.

on_update is a boolean. If it is false (the default value), then slugs will not be updated if a model is resaved (e.g. if you change the title of your blog post, the slug will remain the same) or the slug value has already been set. You can set it to true (or manually change the $model->slug value in your own code) if you want to override this behaviour.

(If you want to manually set the slug value using your model's Sluggable settings, you can run Sluggable::make($model, true). The second arguement forces Sluggable to update the slug field.)

Credits

The idea for this bundle came from using actAs Sluggable from the Doctrine ORM.

laravel-bundle-sluggable's People

Contributors

bryantebeek avatar

Stargazers

Arun S. Sekher avatar Ryun Shofner avatar John D Wells Jr avatar Ricardo Fuhrmann avatar Colin Viebrock avatar

Watchers

James Cloos avatar

laravel-bundle-sluggable's Issues

Unique doesnt seem to be working

The 'unique' option doesnt seem to work for me. I created a test post called 'test' and it was given the slug 'test'. I created a second post with the same title and it was correctly given the 'test-1' slug, but when I created a third post with the same title it was also given the slug 'test-1'

Join forces?

You published your bundle a few hours before me, just as I was wrapping up coding!

https://github.com/cviebrock/sluggable

Besides the fact that you can't have two bundles named "Sluggable", I think it'd be confusing for Laravel users to have two bundles that do pretty much the same thing.

Do you want to combine our code somehow and push it out together?

  • Colin

Option to add unique_on constraint

It would be a great addition to be able to specify an "unique_on" configuration option that allows to specify a field used to restrict the set of elements matching the uniqueness constraint.

This could be used for example to specify a unique slug only for the "blog entries" with the same "author_id" (for routes like /view/author/slug ).

/view/pierlo/test-post

vs

/view/anotherauthor/test-post

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.