Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advanced usage: generate links on tags #9

Open
antonkomarev opened this issue Jan 20, 2015 · 6 comments
Open

Advanced usage: generate links on tags #9

antonkomarev opened this issue Jan 20, 2015 · 6 comments
Assignees

Comments

@antonkomarev
Copy link
Contributor

Here is how I'm using tags without SluggableBehavior.

In application config:

'components' => [
    'urlManager' => [
        'class' => 'yii\web\UrlManager',
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules'=> [
            ['pattern'=>'tag/<tag>', 'route'=>'tag/view'],
        ]
    ],
]

In view file:

<?php foreach ($model->getTagNames(true) as $tag) : ?>
    <?= Html::a($tag, ['/tag/view', 'tag' => $tag]) ?>
<?php endforeach ?>

If we have a tag tag with whitespace we will have a generated link:
/tag/tag+with+whitespace

This link will call TagController:actionView() method and bypass tag parameter in Yii::$app->request->queryParams array.

@creocoder creocoder self-assigned this Jan 24, 2015
@antonkomarev
Copy link
Contributor Author

What is the best way to get tags slugs in a view to bypass them in URL?

@creocoder
Copy link
Owner

@a-komarev As i remember we discuss that already. The best way to do that:

<?php foreach ($model->tags as $tag) : ?>
    <?= Html::a($tag, ['/tag/view', 'tag' => $tag->slug]) ?>
<?php endforeach ?>

@antonkomarev
Copy link
Contributor Author

@creocoder Sorry, looks like I forgot a details.

The correct example will be:

<?php foreach ($model->tags as $tag) : ?>
    <?= Html::a($tag->name, ['/tag/view', 'tag' => $tag->slug]) ?>
<?php endforeach ?>

Because $tag couldn't be passed in Html::a() function, it will be an object in this case.

@antonkomarev
Copy link
Contributor Author

Just realized that all search functions of TaggableQueryBehavior wouldn't be useful in that example. Because all searches are performed by name attribute and not working with slug.

Thats solving in Model which using TaggableBehavior by assigning tagNameAttribute as slug:

[
    'class' => TaggableBehavior::className(),
    'tagNameAttribute' => 'slug',
],

Thanks, Alexander, for flexibility :}

@antonkomarev
Copy link
Contributor Author

My bad. Another problem rising. If you are setting tagNameAttribute as slug search will work, but adding new tags will write them in slug field too :( Do we need to extend TaggableQueryBehavior and add there find by slug functions?

@antonkomarev
Copy link
Contributor Author

Here is a quick solution for SluggableBehavior support: #13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants