Yii Framework Forum: How to use uniqueSlugGenerator? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to use uniqueSlugGenerator?

#1 User is offline   Revocaine 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 27-February 18

Posted 28 February 2018 - 06:45 AM

Hello people!

I am kinda new to Yii2 and PHP and I am having trouble understanding how uniqueSlugGenerator property of SluggableBehavior is used.
I want to generate a unique slug like this,

example.com/user/(randomstring)-username

or

example.com/user/(id)-username

Can anybody please explain with examples if possible?.

my current config looks like this,

        'urlManager' => [
            'baseUrl' => $baseUrl,
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => array(
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            'user/<slug>' => 'user/profile'
            )
        ],


and user model

    public function behaviors()
    {
        return [
            [
                'class' => SluggableBehavior::className(),
                'attribute' => 'username',
                'ensureUnique' => true,
                'immutable' => true
            ],
        ];
    }



Thanks!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users