What's it all about?
With this behavior, you can generate an URI for a single or combination of columns in your table. Some call it permalink, others call it slug or human readable url.
Check out the latest version at: github.com/mintao/yii-behavior-sluggable
| id | category | title | |----+----------+-----------------------------------| | 1 | security | NASA Server hacked by hacker kids | | ...
http://your-blog.org/index.php?r=blog/show&id=1422
http://your-blog.org/security/nasa-server-hacked-by-hacker-kids
Google will love you ;)
How to get it done
If you're using git, I'd recommend:
cd <YOUR YII-PROJECT> mkdir -p protected/extensions/behaviors (optional) git submodule add git://github.com/mintao/yii-behavior-sluggable.git protected/extensions/behaviors/SluggableBehavior
/**
* Behaviors for this model
*/
public function behaviors(){
return array(
'sluggable' => array(
'class'=>'ext.behaviors.SluggableBehavior.SluggableBehavior',
'columns' => array('category', 'title'),
'unique' => true,
'update' => true,
),
);
}
github.com/mintao/yii-behavior-sluggable
Total 20 comments
if I use 'columns' => array('ProductType.slug', 'name'),
I don't get a separator / between the product type and the product, it just all flows on with hyphens?
Sorry if I missing it, but it would be nice having something to exclude words from being slugged.
Something like
Anyway, loving this extension... :)
my sludge looks like: noord-holland-s-graveland. where noord-holland is the province and s-graveland the city. but should it not be like: noord-holland/s-graveland
so how do i have to configur this extension to get i get: noord-holland/s-graveland
Awesome, thanks for this.
Problem with urlManager I am having a problem with getting 'The system is unable to find the requested action' when adding the urlManager rule. The page loads correctly with the query string.
EDIT: Figured it out thanks to this post. The problem was with the regular expression \w not allowing the dash. I changed the rule to the following.
Single Quotes / Apostrophes The slug that is generated was putting dashes in place of apostrophes/single quotes. For example, "person's foobar" would turn into "person-s-foobar". So I changed the urlize function in Doctrine_Inflector.php to remove them before the other preg_replaces are done.
I added the following before "//Remove all none word characters"
my sludge looks like: noord-holland-s-graveland. where noord-holland is the province and s-graveland the city. but should it not be like: noord-holland/s-graveland
in the example it states: http://.../security/nasa-server-hacked-by-hacker-kids
so how i get an url like: http://.../noord-holland/s-graveland
and the count suffix added to the slug is not quite consistent
I created a few articles of the same title and edit the slug fields randomly by removing the "-1" or "-2".. and deleting some articles and add again..
the slugs generated sometimes shows duplicate slugs like
1 "test-slugs"
2 "test-slugs"
3 "test-slugs-2"
or
1 "test-slugs"
2 "test-slugs-2-1"
It doesn't checks the rows properly or assign the right numbers although it will work well with 1 or or 2 records without much editing..
I am new to slug thing..is this how it is meant to be? or do you have plans to further update this extension?
Thanks! great work anyhow...!
Hi mintao, thanks for extension
but can you make it detect duplicate slugs?
currently if I create 2 articles with the same title ("Test Slug") it will generate 2 slugs respectively ("test-slug" and "test-slug-1")
when I edit the artice of "test-slug-1" and change it to "test-slug" it will be accepted and thus there will be 2 "test-slug" in the db
I want to make the Slug field editable in case the user wants to customize it.
how is this handled?
Thanks!
Had to make a few changes, but got it working. Thank you very much for the help. I will also take this opportunity to congratulate you on this excellent extension. Keep up the good work.
If you want to use the slug, set a link like this (Not verified, just hacked into this textarea ;)) Hope you find your way ...
The extension is working for me. The slugs are created and stored in DB.
Now what changes should I make to the urlManager rules to get the slug displayed instead of the id?
I am trying to use this with a Books model (hence tbl_books), with the slug containing only the contents of the column "title". So instead of the url showing
I want it to show
I now this is trivial, but being a Yii newbie I do not know how to set urlManager rules to do this.
Thanks for your help.
Yep
You need to first learn Yii::app()->createUrl
Had one little problem with the extension
When i pull my model from a session i get the following error:
I solved this by adding the following line on top of my model:
When i use Yii::app()->controller->createUrl('page', $model->slug); it create the URL like this:
http://your-blog.org/security-nasa-server-hacked-by-hacker-kids
and not: http://your-blog.org/security/nasa-server-hacked-by-hacker-kids
Did i approach this wrong?
In main.php in UrlManager
you can use
@mintao
Thanks for the reply. As I mentioned earlier, I have got the slugs created and stored in the db and also can access it using $model->slug.
I want to know what to be configured in the url manager in config.php so that it will replace a particular controller/action/id with the 'slug' that has just been created. (What url rules are to be added?)
@ppravin88
@iota and @all
Can i get the url to be in the same form as in the example:
http://your-blog.org/index.php?r=blog/show&id=1422
... so what about ...
http://your-blog.org/security/nasa-server-hacked-by-hacker-kids
ie., the url should be only text( even the id should be hidden)
I got certain options of changing a particular controller/action to a particular text, but the id that comes after that was still visible. How get a url as mentioned in the example?
Thanks!
@ppravin88
I found that the following extension works in well with SluggableBehavior to create your Urls:
Hope that helps :)
hi there.. i have this problem..! slugs are created properly and stored in the db.. but how to replace the url with the slug created..?
If someone (like me) searching for javascript make me slug alternative, you could use javascript code below:
Add some your characters wich you want to replace to "from" string and "to" string. (i added latvian "ā" to replace it with simple "a")
And now use it on title input keyup (jquery example) like
and if you want to prefix it with your url
Excellent idea, excellent practice, excellent tutorial.
"Google will love you ;)"
I love it!
Leave a comment
Please login to leave your comment.